package worker

import "testing"

func TestControllerCoalescedSignalsAllocateNothing(t *testing.T) {
	controller := &Controller{
		wakeCh:      make(chan struct{}, 1),
		reconcileCh: make(chan struct{}, 1),
	}
	controller.Wake()
	controller.RequestReconcile()
	if allocations := testing.AllocsPerRun(10_000, func() {
		controller.Wake()
		controller.RequestReconcile()
	}); allocations != 0 {
		t.Fatalf("coalesced signals allocate %.2f objects per pair", allocations)
	}
}

func BenchmarkControllerWakeCoalesced(b *testing.B) {
	controller := &Controller{wakeCh: make(chan struct{}, 1)}
	controller.Wake()
	b.ReportAllocs()
	b.ResetTimer()
	for range b.N {
		controller.Wake()
	}
}

func BenchmarkControllerReconcileSignalCoalesced(b *testing.B) {
	controller := &Controller{reconcileCh: make(chan struct{}, 1)}
	controller.RequestReconcile()
	b.ReportAllocs()
	b.ResetTimer()
	for range b.N {
		controller.RequestReconcile()
	}
}
