|
| 1 | +package redis |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +const app string = "test-app" |
| 9 | + |
| 10 | +func TestReadFromNonExistingApp(t *testing.T) { |
| 11 | + // Initialize a new storage adapter |
| 12 | + a, err := NewStorageAdapter(10) |
| 13 | + if err != nil { |
| 14 | + t.Error(err) |
| 15 | + } |
| 16 | + // No logs have been written; there should be no redis list for app |
| 17 | + messages, err := a.Read(app, 10) |
| 18 | + if messages != nil { |
| 19 | + t.Error("Expected no messages, but got some") |
| 20 | + } |
| 21 | + if err == nil || err.Error() != fmt.Sprintf("Could not find logs for '%s'", app) { |
| 22 | + t.Error("Did not receive expected error message") |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func TestWithBadBufferSizes(t *testing.T) { |
| 27 | + // Initialize with invalid buffer sizes |
| 28 | + for _, size := range []int{-1, 0} { |
| 29 | + a, err := NewStorageAdapter(size) |
| 30 | + if a != nil { |
| 31 | + t.Error("Expected no storage adapter, but got one") |
| 32 | + } |
| 33 | + if err == nil || err.Error() != fmt.Sprintf("Invalid buffer size: %d", size) { |
| 34 | + t.Error("Did not receive expected error message") |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestLogs(t *testing.T) { |
| 40 | + // Initialize with small buffers |
| 41 | + a, err := NewStorageAdapter(10) |
| 42 | + if err != nil { |
| 43 | + t.Error(err) |
| 44 | + } |
| 45 | + // And write a few logs to it, but do NOT fill it up |
| 46 | + for i := 0; i < 5; i++ { |
| 47 | + if err := a.Write(app, fmt.Sprintf("message %d", i)); err != nil { |
| 48 | + t.Error(err) |
| 49 | + } |
| 50 | + } |
| 51 | + // Read more logs than there are |
| 52 | + messages, err := a.Read(app, 8) |
| 53 | + if err != nil { |
| 54 | + t.Error(err) |
| 55 | + } |
| 56 | + // Should only get as many messages as we actually have |
| 57 | + if len(messages) != 5 { |
| 58 | + t.Errorf("only expected 5 log messages, got %d", len(messages)) |
| 59 | + } |
| 60 | + // Read fewer logs than there are |
| 61 | + messages, err = a.Read(app, 3) |
| 62 | + if err != nil { |
| 63 | + t.Error(err) |
| 64 | + } |
| 65 | + // Should get the 3 MOST RECENT logs |
| 66 | + if len(messages) != 3 { |
| 67 | + t.Errorf("only expected 5 log messages, got %d", len(messages)) |
| 68 | + } |
| 69 | + for i := 0; i < 3; i++ { |
| 70 | + expectedMessage := fmt.Sprintf("message %d", i+2) |
| 71 | + if messages[i] != expectedMessage { |
| 72 | + t.Errorf("expected: \"%s\", got \"%s\"", expectedMessage, messages[i]) |
| 73 | + } |
| 74 | + } |
| 75 | + // Overfill the buffer |
| 76 | + for i := 5; i < 11; i++ { |
| 77 | + if err := a.Write(app, fmt.Sprintf("message %d", i)); err != nil { |
| 78 | + t.Error(err) |
| 79 | + } |
| 80 | + } |
| 81 | + // Read more logs than the buffer can hold |
| 82 | + messages, err = a.Read(app, 20) |
| 83 | + if err != nil { |
| 84 | + t.Error(err) |
| 85 | + } |
| 86 | + // Should only get as many messages as the buffer can hold |
| 87 | + if len(messages) != 10 { |
| 88 | + t.Errorf("only expected 10 log messages, got %d", len(messages)) |
| 89 | + } |
| 90 | + // And they should only be the 10 MOST RECENT logs |
| 91 | + for i := 0; i < 10; i++ { |
| 92 | + expectedMessage := fmt.Sprintf("message %d", i+1) |
| 93 | + if messages[i] != expectedMessage { |
| 94 | + t.Errorf("expected: \"%s\", got \"%s\"", expectedMessage, messages[i]) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestDestroy(t *testing.T) { |
| 100 | + a, err := NewStorageAdapter(10) |
| 101 | + if err != nil { |
| 102 | + t.Error(err) |
| 103 | + } |
| 104 | + // Write a log to create the file |
| 105 | + if err := a.Write(app, "Hello, log!"); err != nil { |
| 106 | + t.Error(err) |
| 107 | + } |
| 108 | + // A redis list should exist for the app |
| 109 | + exists, err := a.redisClient.Exists(app).Result() |
| 110 | + if err != nil { |
| 111 | + t.Error(err) |
| 112 | + } |
| 113 | + if !exists { |
| 114 | + t.Error("Log redis list was expected to exist, but doesn't.") |
| 115 | + } |
| 116 | + // Now destroy it |
| 117 | + if err := a.Destroy(app); err != nil { |
| 118 | + t.Error(err) |
| 119 | + } |
| 120 | + // Now check that the redis list no longer exists |
| 121 | + exists, err = a.redisClient.Exists(app).Result() |
| 122 | + if err != nil { |
| 123 | + t.Error(err) |
| 124 | + } |
| 125 | + if exists { |
| 126 | + t.Error("Log redis list still exist, but was expected not to.") |
| 127 | + } |
| 128 | +} |
0 commit comments