View definition

func SynchronizedBeforeSuite

Defined in


SynchronizedBeforeSuite blocks are primarily meant to solve the problem of setting up singleton external resources shared across nodes when running tests in parallel. For example, say you have a shared database that you can only start one instance of that must be used in your tests. When running in parallel, only one node should set up the database and all other nodes should wait until that node is done before running.

SynchronizedBeforeSuite accomplishes this by taking *two* function arguments. The first is only run on parallel node #1. The second is run on all nodes, but *only* after the first function completes succesfully. Ginkgo also makes it possible to send data from the first function (on Node 1) to the second function (on all the other nodes).

The functions have the following signatures. The first function (which only runs on node 1) has the signature:

func() []byte

or, to run asynchronously:

func(done Done) []byte

The byte array returned by the first function is then passed to the second function, which has the signature:

func(data []byte)

or, to run asynchronously:

func(data []byte, done Done)

Here's a simple pseudo-code example that starts a shared database on Node 1 and shares the database's address with the other nodes:

var dbClient db.Client
var dbRunner db.Runner

var _ = SynchronizedBeforeSuite(func() []byte {
	dbRunner = db.NewRunner()
	err := dbRunner.Start()
	return []byte(dbRunner.URL)
}, func(data []byte) {
	dbClient = db.NewClient()
	err := dbClient.Connect(string(data))