|
| 1 | +package cpa |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net" |
| 9 | + "reflect" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func TestNewRedisQueueDefaultsToUsageChannel(t *testing.T) { |
| 15 | + q := NewRedisQueue(RedisQueueConfig{ |
| 16 | + BaseURL: "http://127.0.0.1:8317", |
| 17 | + ManagementKey: "secret", |
| 18 | + }) |
| 19 | + if q.queueKey != RedisUsageQueueKey { |
| 20 | + t.Fatalf("queueKey = %q, want %q", q.queueKey, RedisUsageQueueKey) |
| 21 | + } |
| 22 | + if RedisUsageQueueKey != "usage" { |
| 23 | + t.Fatalf("RedisUsageQueueKey = %q, want usage", RedisUsageQueueKey) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestPopUsageSendsUsageChannelByDefault(t *testing.T) { |
| 28 | + addr, commands, stop := startRESPUsageServer(t) |
| 29 | + defer stop() |
| 30 | + |
| 31 | + q := NewRedisQueue(RedisQueueConfig{ |
| 32 | + OverrideAddr: addr, |
| 33 | + ManagementKey: "secret", |
| 34 | + Timeout: time.Second, |
| 35 | + BatchSize: 2, |
| 36 | + }) |
| 37 | + got, err := q.PopUsage(context.Background()) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("PopUsage returned error: %v", err) |
| 40 | + } |
| 41 | + if want := []string{`{"id":1}`, `{"id":2}`}; !reflect.DeepEqual(got, want) { |
| 42 | + t.Fatalf("PopUsage = %#v, want %#v", got, want) |
| 43 | + } |
| 44 | + assertCommand(t, <-commands, []string{RedisAuthCommand, "secret"}) |
| 45 | + assertCommand(t, <-commands, []string{RedisLPopCommand, RedisUsageQueueKey, "2"}) |
| 46 | +} |
| 47 | + |
| 48 | +func startRESPUsageServer(t *testing.T) (addr string, commands <-chan []string, stop func()) { |
| 49 | + t.Helper() |
| 50 | + |
| 51 | + ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("listen: %v", err) |
| 54 | + } |
| 55 | + cmdCh := make(chan []string, 2) |
| 56 | + done := make(chan error, 1) |
| 57 | + |
| 58 | + go func() { |
| 59 | + conn, err := ln.Accept() |
| 60 | + if err != nil { |
| 61 | + done <- err |
| 62 | + return |
| 63 | + } |
| 64 | + defer conn.Close() |
| 65 | + |
| 66 | + reader := bufio.NewReader(conn) |
| 67 | + auth, err := readRESPValue(reader) |
| 68 | + if err != nil { |
| 69 | + done <- err |
| 70 | + return |
| 71 | + } |
| 72 | + cmdCh <- auth.strings() |
| 73 | + if _, err := fmt.Fprint(conn, "+OK\r\n"); err != nil { |
| 74 | + done <- err |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + pop, err := readRESPValue(reader) |
| 79 | + if err != nil { |
| 80 | + done <- err |
| 81 | + return |
| 82 | + } |
| 83 | + cmdCh <- pop.strings() |
| 84 | + if _, err := fmt.Fprint(conn, "*2\r\n$8\r\n{\"id\":1}\r\n$8\r\n{\"id\":2}\r\n"); err != nil { |
| 85 | + done <- err |
| 86 | + return |
| 87 | + } |
| 88 | + done <- nil |
| 89 | + }() |
| 90 | + |
| 91 | + stop = func() { |
| 92 | + _ = ln.Close() |
| 93 | + select { |
| 94 | + case err := <-done: |
| 95 | + if err != nil && !isClosedNetErr(err) { |
| 96 | + t.Fatalf("server error: %v", err) |
| 97 | + } |
| 98 | + case <-time.After(2 * time.Second): |
| 99 | + t.Fatal("timeout waiting for RESP test server") |
| 100 | + } |
| 101 | + } |
| 102 | + return ln.Addr().String(), cmdCh, stop |
| 103 | +} |
| 104 | + |
| 105 | +func assertCommand(t *testing.T, got, want []string) { |
| 106 | + t.Helper() |
| 107 | + if !reflect.DeepEqual(got, want) { |
| 108 | + t.Fatalf("command = %#v, want %#v", got, want) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func isClosedNetErr(err error) bool { |
| 113 | + if err == nil { |
| 114 | + return false |
| 115 | + } |
| 116 | + return errors.Is(err, net.ErrClosed) |
| 117 | +} |
0 commit comments