| 86 | } |
| 87 | |
| 88 | func TestMultipleAcksCanGoIntoASingleBatch(t *testing.T) { |
| 89 | ctx := context.Background() |
| 90 | var wg sync.WaitGroup |
| 91 | var mu sync.Mutex |
| 92 | sentAcks := make(map[driver.AckID]int) |
| 93 | ids := []int{1, 2} |
| 94 | ds := &ackingDriverSub{ |
| 95 | q: []*driver.Message{{AckID: ids[0]}, {AckID: ids[1]}}, |
| 96 | sendAcks: func(_ context.Context, ackIDs []driver.AckID) error { |
| 97 | mu.Lock() |
| 98 | defer mu.Unlock() |
| 99 | for _, id := range ackIDs { |
| 100 | sentAcks[id]++ |
| 101 | wg.Done() |
| 102 | } |
| 103 | return nil |
| 104 | }, |
| 105 | } |
| 106 | sub := pubsub.NewSubscription(ds, nil, nil) |
| 107 | defer sub.Shutdown(ctx) |
| 108 | |
| 109 | // Receive and ack the messages concurrently. |
| 110 | for range 2 { |
| 111 | wg.Add(1) |
| 112 | go func() { |
| 113 | mr, err := sub.Receive(ctx) |
| 114 | if err != nil { |
| 115 | t.Error(err) |
| 116 | return |
| 117 | } |
| 118 | mr.Ack() |
| 119 | }() |
| 120 | } |
| 121 | wg.Wait() |
| 122 | |
| 123 | if len(sentAcks) != 2 { |
| 124 | t.Errorf("len(sentAcks) = %d, want exactly 2", len(sentAcks)) |
| 125 | } |
| 126 | for _, id := range ids { |
| 127 | if sentAcks[id] != 1 { |
| 128 | t.Errorf("sentAcks[%v] = %d, want 1", id, sentAcks[id]) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func TestTooManyAcksForASingleBatchGoIntoMultipleBatches(t *testing.T) { |
| 134 | ctx := context.Background() |