|
| 1 | +package safeacks |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/onsi/ginkgo/v2" |
| 5 | + . "github.com/onsi/gomega" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +var _ = Describe("SafeAcks", func() { |
| 12 | + var sa *SafeAcks |
| 13 | + |
| 14 | + BeforeEach(func() { |
| 15 | + sa = NewAcks() |
| 16 | + }) |
| 17 | + |
| 18 | + Describe("RegisterAck", func() { |
| 19 | + It("should register a new ack", func(ctx SpecContext) { |
| 20 | + err := sa.RegisterAck("test") |
| 21 | + Expect(err).NotTo(HaveOccurred()) |
| 22 | + Expect(sa.ExpectsAck("test")).To(BeTrue()) |
| 23 | + }, SpecTimeout(5*time.Second)) |
| 24 | + |
| 25 | + It("should return error when an ack is already registered", func(ctx SpecContext) { |
| 26 | + err := sa.RegisterAck("test") |
| 27 | + Expect(err).NotTo(HaveOccurred()) |
| 28 | + Expect(sa.ExpectsAck("test")).To(BeTrue()) |
| 29 | + |
| 30 | + err = sa.RegisterAck("test") |
| 31 | + Expect(err).To(HaveOccurred()) |
| 32 | + |
| 33 | + Expect(sa.ExpectsAck("test")).To(BeTrue()) |
| 34 | + }, SpecTimeout(5*time.Second)) |
| 35 | + }) |
| 36 | + // TODO add timeout for this test |
| 37 | + Describe("TrySendAck and TryReceiveAck", func() { |
| 38 | + It("should return nil for non-existent key", func(ctx SpecContext) { |
| 39 | + err := sa.TrySendAck("nonexistent") |
| 40 | + Expect(err).To(BeNil()) |
| 41 | + }, SpecTimeout(5*time.Second)) |
| 42 | + |
| 43 | + It("should send ack successfully", func(ctx SpecContext) { |
| 44 | + err := sa.RegisterAck("test") |
| 45 | + Expect(err).NotTo(HaveOccurred()) |
| 46 | + |
| 47 | + var wg sync.WaitGroup |
| 48 | + wg.Add(1) |
| 49 | + |
| 50 | + go func() { |
| 51 | + defer wg.Done() |
| 52 | + err := sa.TrySendAck("test") |
| 53 | + Expect(err).To(BeNil()) |
| 54 | + }() |
| 55 | + Expect(sa.TryReceiveAck("test")).To(BeTrue()) |
| 56 | + |
| 57 | + wg.Wait() |
| 58 | + }, SpecTimeout(5*time.Second)) |
| 59 | + |
| 60 | + It("should return error when ack was already sent once", func(ctx SpecContext) { |
| 61 | + err := sa.RegisterAck("test") |
| 62 | + Expect(err).NotTo(HaveOccurred()) |
| 63 | + |
| 64 | + result1 := make(chan error) |
| 65 | + result2 := make(chan error) |
| 66 | + go func() { |
| 67 | + result1 <- sa.TrySendAck("test") |
| 68 | + }() |
| 69 | + |
| 70 | + go func() { |
| 71 | + result2 <- sa.TrySendAck("test") |
| 72 | + }() |
| 73 | + |
| 74 | + // I really don't like relying on a sleep call to test this, but I see no other way... |
| 75 | + // The goal is to have both `TrySendAck` blocked at channel send before invoking TryReceiveAck. |
| 76 | + // Hopefully 1 second is enough to avoid having a shaky test. |
| 77 | + time.Sleep(1000 * time.Millisecond) |
| 78 | + |
| 79 | + ok := sa.TryReceiveAck("test") |
| 80 | + Expect(ok).To(BeTrue()) |
| 81 | + |
| 82 | + oneErrorHaveOccured := (<-result1 != nil) != (<-result2 != nil) |
| 83 | + Expect(oneErrorHaveOccured).To(BeTrue()) |
| 84 | + }, SpecTimeout(5*time.Second)) |
| 85 | + }) |
| 86 | + |
| 87 | + Describe("ExpectsAck", func() { |
| 88 | + It("should return false for non-existent key", func(ctx SpecContext) { |
| 89 | + Expect(sa.ExpectsAck("nonexistent")).To(BeFalse()) |
| 90 | + }, SpecTimeout(5*time.Second)) |
| 91 | + |
| 92 | + It("should return true for registered key", func(ctx SpecContext) { |
| 93 | + err := sa.RegisterAck("test") |
| 94 | + Expect(err).NotTo(HaveOccurred()) |
| 95 | + Expect(sa.ExpectsAck("test")).To(BeTrue()) |
| 96 | + }, SpecTimeout(5*time.Second)) |
| 97 | + |
| 98 | + It("should not be permanently blocked by another call", func(ctx SpecContext) { |
| 99 | + err := sa.RegisterAck("test") |
| 100 | + Expect(err).NotTo(HaveOccurred()) |
| 101 | + go func() { |
| 102 | + sa.TryReceiveAck("test") |
| 103 | + }() |
| 104 | + |
| 105 | + // I really don't like relying on a sleep call to test this, but I see no other way... |
| 106 | + // The goal is to have `TryReceiveAck` blocked at channel receive before invoking ExpectsAck. |
| 107 | + // Hopefully 1 second is enough to avoid having a shaky test. |
| 108 | + time.Sleep(1000 * time.Millisecond) |
| 109 | + |
| 110 | + Expect(sa.ExpectsAck("test")).To(BeTrue()) |
| 111 | + }, SpecTimeout(5*time.Second)) |
| 112 | + }) |
| 113 | +}) |
| 114 | + |
| 115 | +func TestSafeAcks(t *testing.T) { |
| 116 | + RegisterFailHandler(Fail) |
| 117 | + RunSpecs(t, "Component SafeAcks Test Suite") |
| 118 | +} |
0 commit comments