|
| 1 | +package resp |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +// TestReadMessage tests the readMessage function for complete and partial reads. |
| 12 | +func TestReadMessage(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + input string |
| 15 | + want string |
| 16 | + wantErr bool |
| 17 | + }{ |
| 18 | + // Single-element array (PING) |
| 19 | + {"*1\r\n$4\r\nPING\r\n", "*1\r\n$4\r\nPING\r\n", false}, |
| 20 | + // Two-element array (ECHO hello) |
| 21 | + {"*2\r\n$4\r\nECHO\r\n$5\r\nhello\r\n", "*2\r\n$4\r\nECHO\r\n$5\r\nhello\r\n", false}, |
| 22 | + // Three-element array (SET key value) |
| 23 | + {"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n", "*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n", false}, |
| 24 | + // Non-array first byte — returned as-is for ParseCommand to reject |
| 25 | + {"+OK\r\n", "+OK\r\n", false}, |
| 26 | + } |
| 27 | + for _, test := range tests { |
| 28 | + reader := bufio.NewReader(strings.NewReader(test.input)) |
| 29 | + got, err := readMessage(reader) |
| 30 | + if test.wantErr && err == nil { |
| 31 | + t.Errorf("readMessage(%q) expected error, got nil", test.input) |
| 32 | + continue |
| 33 | + } |
| 34 | + if !test.wantErr && err != nil { |
| 35 | + t.Errorf("readMessage(%q) unexpected error: %v", test.input, err) |
| 36 | + continue |
| 37 | + } |
| 38 | + if string(got) != test.want { |
| 39 | + t.Errorf("readMessage(%q) = %q; want %q", test.input, got, test.want) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// TestReadMessage_MultipleCommands tests that readMessage reads one command at |
| 45 | +// a time from a stream containing multiple pipelined commands. |
| 46 | +func TestReadMessage_MultipleCommands(t *testing.T) { |
| 47 | + // Two commands sent back-to-back in the same stream (pipelining) |
| 48 | + input := "*1\r\n$4\r\nPING\r\n" + |
| 49 | + "*2\r\n$4\r\nECHO\r\n$5\r\nhello\r\n" |
| 50 | + |
| 51 | + reader := bufio.NewReader(strings.NewReader(input)) |
| 52 | + |
| 53 | + first, err := readMessage(reader) |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("first readMessage error: %v", err) |
| 56 | + } |
| 57 | + if string(first) != "*1\r\n$4\r\nPING\r\n" { |
| 58 | + t.Errorf("first message = %q; want %q", first, "*1\r\n$4\r\nPING\r\n") |
| 59 | + } |
| 60 | + |
| 61 | + second, err := readMessage(reader) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("second readMessage error: %v", err) |
| 64 | + } |
| 65 | + if string(second) != "*2\r\n$4\r\nECHO\r\n$5\r\nhello\r\n" { |
| 66 | + t.Errorf("second message = %q; want %q", second, "*2\r\n$4\r\nECHO\r\n$5\r\nhello\r\n") |
| 67 | + } |
| 68 | + |
| 69 | + // Third read should return EOF |
| 70 | + _, err = readMessage(reader) |
| 71 | + if err != io.EOF { |
| 72 | + t.Errorf("expected io.EOF after last command, got %v", err) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// TestReadMessage_SplitAcrossReads simulates a command arriving in two separate |
| 77 | +// TCP chunks by writing to a pipe with a delay between writes. |
| 78 | +func TestReadMessage_SplitAcrossReads(t *testing.T) { |
| 79 | + // Simulate split delivery: write two parts with a pipe |
| 80 | + pr, pw := io.Pipe() |
| 81 | + reader := bufio.NewReader(pr) |
| 82 | + |
| 83 | + done := make(chan []byte, 1) |
| 84 | + go func() { |
| 85 | + msg, _ := readMessage(reader) |
| 86 | + done <- msg |
| 87 | + }() |
| 88 | + |
| 89 | + // Write the first half |
| 90 | + pw.Write([]byte("*3\r\n$3\r\nSET\r\n")) |
| 91 | + // Write the second half |
| 92 | + pw.Write([]byte("$3\r\nkey\r\n$5\r\nvalue\r\n")) |
| 93 | + pw.Close() |
| 94 | + |
| 95 | + got := <-done |
| 96 | + want := "*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n" |
| 97 | + if !bytes.Equal(got, []byte(want)) { |
| 98 | + t.Errorf("split read = %q; want %q", got, want) |
| 99 | + } |
| 100 | +} |
0 commit comments