onsi / ginkgo

A Modern Testing Framework for Go
http://onsi.github.io/ginkgo/
MIT License
8.22k stars 650 forks source link

Question: How can I provide different state to each parallel process when using SynchronizeBeforeSuite? #1276

Closed cam-schultz closed 11 months ago

cam-schultz commented 11 months ago

Hi all,

I'm looking into running suites in parallel using SynchronizeBeforeSuite. I'd like to provide each parallel process with an element from a pre-generated list of data (e.g. I can hardcode this at global scope, or set it in the process1 function of SynchronizeBeforeSuite). It is not important which process gets which element, as long as no two processes gets the same element. Suites can use the same element as a previous suite running on the same process; the only important thing is that no element from that list is used simultaneously.

Given those requirement, what I think I want to do is assign a unique element from the list in each allProcesses invocation in SynchronizeBeforeSuite, but I'm not sure how to go about doing so. From what I have researched, Ginkgo doesn't provide a way to associate an ID with a parallel process a priori. Is my understanding correct? If so, does Ginkgo provide any mechanism for providing different state to different parallel processes?

P.S. Thanks for authoring and maintaining such a powerful testing framework :)

onsi commented 11 months ago

hey there! yes this is possible: each parallel process has a unique process index. You can get it via GinkgoParallelProcess() which will return an integer. the list is 1-indexed so if you have 8 processes GinkgoParallelProcess() will return 1 through 8.

If you want to get the total number of processes you can use:

config, _ := GinkgoConfiguration()
config.ParallelTotal //this is the total number of processes

you can use config.ParallelTotal to set up the list in SynchronizedBeforeSuite and then use GinkgoParallelProcess() to index into it on each process.

there is some more in-depth discussion of this stuff in the docs here but hopefully this will get you started

cam-schultz commented 11 months ago

Fantastic, thank you for your response! This fits my use case exactly. Closing this issue as resolved.