Exemplo n.º 1
0
func TestJobQueuePushWaitStress(t *testing.T) {
	RegisterTestingT(t)

	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	pushed := set.New()
	popped := set.New()

	q := newJobListQueue()
	Ω(q).ShouldNot(BeNil())

	wg := sync.WaitGroup{}

	N := 50 * 1000
	wg.Add(N)

	for i := 0; i < N; i++ {
		pusher, item := i%2 == 0, i/2

		if pusher {
			go func(item int) {
				defer wg.Done()
				pushed.Add(item)
				q.Push(item)
			}(item)
		} else {
			go func() {
				defer wg.Done()
				item, ok := q.WaitForJob()
				Ω(ok).ShouldNot(BeFalse())
				popped.Add(item)
			}()
		}
	}

	wg.Wait()

	unpopped := set.Difference(pushed, popped)
	Ω(set.IntSlice(unpopped)).Should(BeEmpty())
	unpushed := set.Difference(popped, pushed)
	Ω(set.IntSlice(unpushed)).Should(BeEmpty())
	Ω(set.IntSlice(pushed)).Should(HaveLen(N / 2))
	Ω(set.IntSlice(popped)).Should(HaveLen(N / 2))
}
Exemplo n.º 2
0
func TestQueueStress(t *testing.T) {
	RegisterTestingT(t)

	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	addedJobs := set.New()
	processedJobs := set.New()

	q := New("stress", 100, func(j Job) {
		time.Sleep(3 * time.Millisecond)
		processedJobs.Add(j)
	})

	Ω(q.Start()).Should(BeNil())

	N := 5 * 1000
	wg := sync.WaitGroup{}
	wg.Add(N)
	for i := 0; i < N; i++ {
		go func(item int) {
			defer wg.Done()
			addedJobs.Add(item)
			Ω(q.Add(item)).Should(BeNil())
		}(i)
	}
	wg.Wait() // Wait while all jobs are added to the queue

	Ω(q.Stop(true)).Should(BeNil())

	unprocessed := set.Difference(addedJobs, processedJobs)
	Ω(set.IntSlice(unprocessed)).Should(BeEmpty())
	unadded := set.Difference(processedJobs, addedJobs)
	Ω(set.IntSlice(unadded)).Should(BeEmpty())
	Ω(set.IntSlice(addedJobs)).Should(HaveLen(N))
	Ω(set.IntSlice(processedJobs)).Should(HaveLen(N))
}