|
1 | 1 | package taskq_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "context"
|
5 | 6 | "errors"
|
6 | 7 | "fmt"
|
| 8 | + "io" |
| 9 | + "log" |
7 | 10 | "math/rand"
|
| 11 | + "os" |
8 | 12 | "runtime"
|
9 | 13 | "strconv"
|
10 | 14 | "strings"
|
@@ -92,6 +96,77 @@ func testConsumer(t *testing.T, factory taskq.Factory, opt *taskq.QueueOptions)
|
92 | 96 | }
|
93 | 97 | }
|
94 | 98 |
|
| 99 | +func testConsumerDelete(t *testing.T, factory taskq.Factory, opt *taskq.QueueOptions) { |
| 100 | + old := os.Stderr |
| 101 | + r, w, err := os.Pipe() |
| 102 | + if err != nil { |
| 103 | + log.Fatal(err) |
| 104 | + } |
| 105 | + os.Stderr = w |
| 106 | + |
| 107 | + taskq.SetLogger(log.New(os.Stderr, "taskq: ", log.LstdFlags|log.Lshortfile)) |
| 108 | + |
| 109 | + c := context.Background() |
| 110 | + opt.WaitTimeout = waitTimeout |
| 111 | + opt.Redis = redisRing() |
| 112 | + |
| 113 | + q := factory.RegisterQueue(opt) |
| 114 | + defer q.Close() |
| 115 | + |
| 116 | + purge(t, q) |
| 117 | + |
| 118 | + outC := make(chan string) |
| 119 | + go func() { |
| 120 | + var buf bytes.Buffer |
| 121 | + io.Copy(&buf, r) |
| 122 | + outC <- buf.String() |
| 123 | + }() |
| 124 | + |
| 125 | + ch := make(chan time.Time) |
| 126 | + task := taskq.RegisterTask(&taskq.TaskOptions{ |
| 127 | + Name: nextTaskID(), |
| 128 | + Handler: func() error { |
| 129 | + ch <- time.Now() |
| 130 | + return nil |
| 131 | + }, |
| 132 | + }) |
| 133 | + |
| 134 | + err = q.Add(task.WithArgs(c)) |
| 135 | + if err != nil { |
| 136 | + t.Fatal(err) |
| 137 | + } |
| 138 | + |
| 139 | + p := q.Consumer() |
| 140 | + if err := p.Start(c); err != nil { |
| 141 | + t.Fatal(err) |
| 142 | + } |
| 143 | + |
| 144 | + select { |
| 145 | + case <-ch: |
| 146 | + case <-time.After(testTimeout): |
| 147 | + t.Fatalf("message was not processed") |
| 148 | + } |
| 149 | + |
| 150 | + time.Sleep(2 * time.Second) |
| 151 | + w.Close() |
| 152 | + os.Stderr = old |
| 153 | + |
| 154 | + out := <-outC |
| 155 | + findPendingLogLine := strings.Contains(out, "redisq: pending failed: redisq: can't find pending message") |
| 156 | + |
| 157 | + if findPendingLogLine { |
| 158 | + t.Fatalf("data not acknowledged and still exists in pending list.") |
| 159 | + } |
| 160 | + |
| 161 | + if err := p.Stop(); err != nil { |
| 162 | + t.Fatal(err) |
| 163 | + } |
| 164 | + |
| 165 | + if err := q.Close(); err != nil { |
| 166 | + t.Fatal(err) |
| 167 | + } |
| 168 | +} |
| 169 | + |
95 | 170 | func testUnknownTask(t *testing.T, factory taskq.Factory, opt *taskq.QueueOptions) {
|
96 | 171 | c := context.Background()
|
97 | 172 | opt.WaitTimeout = waitTimeout
|
|
0 commit comments