For some test plans, it is useful to pass information from one instance to another. In addition to direct network connectivity, test plans can pass information between instances using the Testground sync service
.
In this tutorial, we will explore typed message passing through the Testground sync service.
Lets create a plan in which one of the plans produces a struct
which is re-constructed on the distant end. First, I'll show short snippets with the relevant information, and the whole test plan will be shown at the end.
Transferrable
is the value type we will be transferring.
type Transferrable struct {Name stringFavoriteSport intCareWhoKnows bool}
The value will be transferred over a topic
. Think of the topic
as a named and typed channel for transferring values between plan instances. This topic is named transfer-key
and the value type I expect to get out of it is pointer to Transferrable
.
st := sync.NewTopic("transfer-key", &Transferrable{})
To write to a topic, create a bounded client and use it to publish to the topic we have just defined.
ctx := context.Background()client := sync.MustBoundClient(ctx, runenv)defer client.Close()​client.Publish(ctx, st, &Transferrable{"Guy#1", 1, false})
Subscribe to the topic we created earlier and set up a channel to receive the values.
tch := make(chan *Transferrable)_, err = client.Subscribe(ctx, st, tch)if err != nil {panic(err)}
This question is left up to the plan writer, and certainly different situations will call for different implementations. In this example, all the plans will publish and all will subscribe, but there are scenarios where this is inappropriate.
package main​import ("context""fmt""math/rand""time"​"github.com/testground/sdk-go/runtime""github.com/testground/sdk-go/sync")​type Sport int​const (football Sport = iotatennishockeygolf)​func (s Sport) String() string {return [...]string{"football", "tennis", "hockey", "golf"}[s]}​type Transferrable struct {Name stringFavoriteSport SportCareWhoKnows bool}​func (t *Transferrable) String() string {msg := fmt.Sprintf("%s: I like %s", t.Name, t.FavoriteSport)if t.CareWhoKnows {return msg + " and I really care!"}return msg + " and I don't care who knows!"}​func main() {runtime.Invoke(run)}​func run(runenv *runtime.RunEnv) error {rand.Seed(time.Now().UnixNano())​ctx := context.Background()client := sync.MustBoundClient(ctx, runenv)defer client.Close()st := sync.NewTopic("transfer-key", &Transferrable{})​// Configure the testmyName := fmt.Sprintf("Guy#%d", rand.Int()%100)mySport := Sport(rand.Int() % 4)howMany := runenv.TestInstanceCount​// Publish my entryclient.Publish(ctx, st, &Transferrable{myName, mySport, false})​// Wait until all instances have published entriesreadyState := sync.State("ready")client.MustSignalEntry(ctx, readyState)<-client.MustBarrier(ctx, readyState, howMany).C​// Subscribe to the `transfer-key` topictch := make(chan *Transferrable)client.Subscribe(ctx, st, tch)​for i := 0; i < howMany; i++ {t := <-tchrunenv.RecordMessage("%s", t)}​return nil}
Run with multiple instances:
$ testground run single -p quickstart -t quickstart -b exec:go -r local:exec -i 4
Notice that instances is set to 4. Four instances will run at the same time.