Skip to content

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

@MaximeWeyl

Description

@MaximeWeyl

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))
	})
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions