// New instantiates a running MetricBatcher. Eventswill be emitted once per batchDuration. All // updates to a given counter name will be combined into a single event and sent to metricSender. func New(metricSender metric_sender.MetricSender, batchDuration time.Duration) *MetricBatcher { mb := &MetricBatcher{ metrics: make(map[string]uint64), batchTicker: time.NewTicker(batchDuration), metricSender: metricSender, } go func() { for { <-mb.batchTicker.C mb.lock.Lock() for name, delta := range mb.metrics { metricSender.AddToCounter(name, delta) } mb.unsafeReset() mb.lock.Unlock() } }() return mb }
Expect(emitter.GetMessages()).To(HaveLen(0)) Expect(err.Error()).To(Equal("some error")) }) It("sends an update counter event to its emitter", func() { err := sender.IncrementCounter("counter-strike") Expect(err).NotTo(HaveOccurred()) Expect(emitter.GetMessages()).To(HaveLen(1)) counterEvent := emitter.GetMessages()[0].Event.(*events.CounterEvent) Expect(counterEvent.GetName()).To(Equal("counter-strike")) Expect(counterEvent.GetDelta()).To(Equal(uint64(1))) }) It("sends an update counter event with arbitrary increment", func() { err := sender.AddToCounter("counter-strike", 3) Expect(err).NotTo(HaveOccurred()) Expect(emitter.GetMessages()).To(HaveLen(1)) counterEvent := emitter.GetMessages()[0].Event.(*events.CounterEvent) Expect(counterEvent.GetName()).To(Equal("counter-strike")) Expect(counterEvent.GetDelta()).To(Equal(uint64(3))) }) It("returns an error if it can't increment counter", func() { emitter.ReturnError = errors.New("some counter event error") err := sender.IncrementCounter("count me in") Expect(emitter.GetMessages()).To(HaveLen(0)) Expect(err.Error()).To(Equal("some counter event error")) })