Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gmeasure.Sample does not wait for running jobs when run in parallel #803

Closed
MaximeWeyl opened this issue Dec 9, 2024 · 0 comments · Fixed by #804
Closed

gmeasure.Sample does not wait for running jobs when run in parallel #803

MaximeWeyl opened this issue Dec 9, 2024 · 0 comments · Fixed by #804

Comments

@MaximeWeyl
Copy link

gmeasure.Sample does not wait for running jobs when run in parallel. It launches the last jobs and then exits as soon as the conditions are met, without waiting for the last running jobs to finish.

Here is a simple test suite to showcase the issue.

The first test would be expected to pass, but does not.
The second test is a workaround that make it pass.

package testginkgosample_test

import (
	. "github.com/onsi/gomega/gmeasure"
	"sync"
	"testing"
	"time"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
)

func TestTestginkgosample(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Testginkgosample Suite")
}

var _ = Describe("gmeasure.Sample", func() {
	var N int

	BeforeEach(func() {
		N = 12
	})

	It("should run all started functions", func() { //but fails
		exp := NewExperiment("Testginkgosample")

		var counter int
		var counterLock sync.Mutex

		exp.Sample(func(i int) {
			time.Sleep(1 * time.Second)
			counterLock.Lock()
			counter++
			counterLock.Unlock()
		}, SamplingConfig{
			N:           N,
			NumParallel: 5,
		})

		Expect(counter).To(Equal(N))
	})

	It("actually needs a wait group", func() {
		exp := NewExperiment("Testginkgosample")

		var counter int
		var counterLock sync.Mutex
		var wg sync.WaitGroup

		exp.Sample(func(i int) {
			wg.Add(1)
			time.Sleep(1 * time.Second)
			counterLock.Lock()
			counter++
			counterLock.Unlock()
			wg.Done()
		}, SamplingConfig{
			N:           N,
			NumParallel: 5,
		})

		wg.Wait()

		Expect(counter).To(Equal(N))
	})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant