import (
	"sync/atomic"
	"time"

	"tools/benchmark/writestrategies"

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

var _ = Describe("WriteStrategies", func() {
	Context("ConstantWriteStrategy", func() {
		Describe("StartWriter", func() {
			var (
				writer        mockWriter
				stopChan      chan struct{}
				writeStrategy *writestrategies.ConstantWriteStrategy
			)

			BeforeEach(func() {
				writer = mockWriter{}
				stopChan = make(chan struct{})
				writeStrategy = writestrategies.NewConstantWriteStrategy(&mockGenerator{}, &writer, 1000)
			})

			It("writes messages at a constant rate", func() {
				go writeStrategy.StartWriter(stopChan)

				time.Sleep(time.Millisecond * 50)
				writes := atomic.LoadUint32(&writer.count)
				Expect(writes).To(BeNumerically(">", 40))
import (
	"sync/atomic"
	"time"

	"tools/benchmark/writestrategies"

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

var _ = Describe("WriteStrategies", func() {
	Context("ConstantWriteStrategy", func() {
		Describe("StartWriter", func() {
			var (
				writer        mockWriter
				writeStrategy *writestrategies.ConstantWriteStrategy
			)

			BeforeEach(func() {
				writer = mockWriter{}
				writeStrategy = writestrategies.NewConstantWriteStrategy(&mockGenerator{}, &writer, 1000)
			})

			Measure("writes messages at a constant rate", func(b Benchmarker) {
				go writeStrategy.StartWriter()

				time.Sleep(time.Millisecond * 50)
				writes := atomic.LoadUint32(&writer.count)
				Expect(writes).To(BeNumerically("~", 50, 10))

				time.Sleep(time.Millisecond * 50)