|
| 1 | +package relay_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/pyroscope-io/pyroscope-lambda-extension/relay" |
| 5 | + "github.com/sirupsen/logrus" |
| 6 | + "net/http" |
| 7 | + "sync" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +type asyncJob struct { |
| 13 | + name string |
| 14 | + m sync.Mutex |
| 15 | + t *testing.T |
| 16 | +} |
| 17 | + |
| 18 | +func newAsyncJob(t *testing.T, name string, f func()) *asyncJob { |
| 19 | + res := &asyncJob{t: t, name: name} |
| 20 | + res.m.Lock() |
| 21 | + go func() { |
| 22 | + f() |
| 23 | + res.m.Unlock() |
| 24 | + }() |
| 25 | + return res |
| 26 | +} |
| 27 | + |
| 28 | +func (j *asyncJob) assertNotFinished() { |
| 29 | + locked := j.m.TryLock() |
| 30 | + if locked { |
| 31 | + j.t.Fatalf("should be still working... " + j.name) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (j *asyncJob) assertFinished() { |
| 36 | + j.m.Lock() |
| 37 | +} |
| 38 | + |
| 39 | +type flushTestHelper struct { |
| 40 | + t *testing.T |
| 41 | + log *logrus.Entry |
| 42 | + responses chan struct{} |
| 43 | + requests chan struct{} |
| 44 | + req *http.Request |
| 45 | + queue *relay.RemoteQueue |
| 46 | +} |
| 47 | + |
| 48 | +func newFlushMockRelay(t *testing.T) *flushTestHelper { |
| 49 | + req, _ := http.NewRequest(http.MethodPost, "/", nil) |
| 50 | + log := logrus.WithFields(logrus.Fields{"svc": "flush-test"}) |
| 51 | + res := &flushTestHelper{ |
| 52 | + t: t, |
| 53 | + log: log, |
| 54 | + responses: make(chan struct{}, 128), |
| 55 | + requests: make(chan struct{}, 128), |
| 56 | + req: req, |
| 57 | + } |
| 58 | + res.queue = relay.NewRemoteQueue(log, &relay.RemoteQueueCfg{ |
| 59 | + NumWorkers: 2, |
| 60 | + }, res) |
| 61 | + logrus.SetLevel(logrus.DebugLevel) |
| 62 | + |
| 63 | + return res |
| 64 | +} |
| 65 | + |
| 66 | +func (h *flushTestHelper) Send(_ *http.Request) error { |
| 67 | + //h.log.Debug("flushTestHelper.send 1") |
| 68 | + h.requests <- struct{}{} |
| 69 | + //h.log.Debug("flushTestHelper.send 2") |
| 70 | + <-h.responses |
| 71 | + //h.log.Debug("flushTestHelper.send 3") |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (h *flushTestHelper) respond() { |
| 76 | + h.responses <- struct{}{} |
| 77 | +} |
| 78 | + |
| 79 | +func (h *flushTestHelper) flushAsync() *asyncJob { |
| 80 | + return newAsyncJob(h.t, "flush", func() { |
| 81 | + h.queue.Flush() |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func (h *flushTestHelper) sendAsync() *asyncJob { |
| 86 | + return newAsyncJob(h.t, "send", func() { |
| 87 | + _ = h.queue.Send(h.req) |
| 88 | + }) |
| 89 | +} |
| 90 | +func (h *flushTestHelper) send() { |
| 91 | + _ = h.queue.Send(h.req) |
| 92 | +} |
| 93 | + |
| 94 | +func (h *flushTestHelper) step() { |
| 95 | + time.Sleep(100 * time.Millisecond) |
| 96 | +} |
| 97 | + |
| 98 | +func (h *flushTestHelper) assertRequestsProcessed(n int) { |
| 99 | + if n != len(h.requests) { |
| 100 | + h.t.Fatalf("expected %d got %d", n, len(h.responses)) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestFlushWaitsForAllEnqueuedRequests(t *testing.T) { |
| 105 | + n := 3 |
| 106 | + h := newFlushMockRelay(t) |
| 107 | + _ = h.queue.Start() |
| 108 | + for i := 0; i < n; i++ { |
| 109 | + h.send() |
| 110 | + } |
| 111 | + f := h.flushAsync() |
| 112 | + for i := 0; i < n; i++ { |
| 113 | + h.step() |
| 114 | + f.assertNotFinished() |
| 115 | + h.respond() |
| 116 | + } |
| 117 | + f.assertFinished() |
| 118 | + h.assertRequestsProcessed(n) |
| 119 | +} |
| 120 | + |
| 121 | +func TestFlushWaitsForAllEnqueuedRequestsWhenQueueIsFullAndSomeAreDropped(t *testing.T) { |
| 122 | + n := 30 |
| 123 | + h := newFlushMockRelay(t) |
| 124 | + //queueSize := cap(h.queue.jobs) |
| 125 | + queueSize := 20 |
| 126 | + for i := 0; i < n; i++ { //send 30, 10 are dropped |
| 127 | + h.send() |
| 128 | + } |
| 129 | + _ = h.queue.Start() |
| 130 | + f := h.flushAsync() |
| 131 | + for i := 0; i < queueSize; i++ { //20 are processed |
| 132 | + h.step() |
| 133 | + f.assertNotFinished() |
| 134 | + h.respond() |
| 135 | + } |
| 136 | + f.assertFinished() |
| 137 | + h.assertRequestsProcessed(queueSize) |
| 138 | +} |
| 139 | + |
| 140 | +func TestFlushWithQueueEmpty(t *testing.T) { |
| 141 | + h := newFlushMockRelay(t) |
| 142 | + _ = h.queue.Start() |
| 143 | + f := h.flushAsync() |
| 144 | + f.assertFinished() |
| 145 | + h.assertRequestsProcessed(0) |
| 146 | +} |
| 147 | + |
| 148 | +func TestFlushSendEventDuringFlushBlocks(t *testing.T) { |
| 149 | + n := 3 |
| 150 | + h := newFlushMockRelay(t) |
| 151 | + _ = h.queue.Start() |
| 152 | + for i := 0; i < n; i++ { |
| 153 | + h.send() |
| 154 | + } |
| 155 | + f := h.flushAsync() |
| 156 | + h.step() |
| 157 | + s := h.sendAsync() |
| 158 | + for i := 0; i < n; i++ { |
| 159 | + h.step() |
| 160 | + f.assertNotFinished() |
| 161 | + s.assertNotFinished() |
| 162 | + } |
| 163 | + for i := 0; i < n; i++ { |
| 164 | + h.respond() |
| 165 | + } |
| 166 | + f.assertFinished() |
| 167 | + s.assertFinished() |
| 168 | + |
| 169 | +} |
0 commit comments