github.com/anycable/anycable-go@v1.5.1/broker/memory_test.go (about) 1 package broker 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestMemory_Expire(t *testing.T) { 12 config := NewConfig() 13 config.HistoryTTL = 1 14 15 broker := NewMemoryBroker(nil, &config) 16 17 start := time.Now().Unix() - 10 18 19 broker.add("test", "a") 20 broker.add("test", "b") 21 22 time.Sleep(2 * time.Second) 23 24 broker.add("test", "c") 25 broker.add("test", "d") 26 27 broker.expire() 28 29 history, err := broker.HistorySince("test", start) 30 require.NoError(t, err) 31 32 assert.Len(t, history, 2) 33 assert.EqualValues(t, 3, history[0].Offset) 34 assert.Equal(t, "c", history[0].Data) 35 assert.EqualValues(t, 4, history[1].Offset) 36 assert.Equal(t, "d", history[1].Data) 37 38 time.Sleep(2 * time.Second) 39 40 broker.expire() 41 42 history, err = broker.HistorySince("test", start) 43 require.NoError(t, err) 44 assert.Empty(t, history) 45 } 46 47 func TestMemory_Limit(t *testing.T) { 48 config := NewConfig() 49 config.HistoryLimit = 2 50 51 broker := NewMemoryBroker(nil, &config) 52 53 start := time.Now().Unix() - 10 54 55 broker.add("test", "a") 56 broker.add("test", "b") 57 broker.add("test", "c") 58 59 history, err := broker.HistorySince("test", start) 60 require.NoError(t, err) 61 62 assert.Len(t, history, 2) 63 assert.EqualValues(t, 3, history[1].Offset) 64 assert.Equal(t, "c", history[1].Data) 65 } 66 67 func TestMemory_FromOffset(t *testing.T) { 68 config := NewConfig() 69 70 broker := NewMemoryBroker(nil, &config) 71 72 broker.add("test", "a") 73 broker.add("test", "b") 74 broker.add("test", "c") 75 broker.add("test", "d") 76 broker.add("test", "e") 77 78 t.Run("With current epoch", func(t *testing.T) { 79 history, err := broker.HistoryFrom("test", broker.epoch, 2) 80 require.NoError(t, err) 81 82 assert.Len(t, history, 3) 83 assert.EqualValues(t, 3, history[0].Offset) 84 assert.Equal(t, "c", history[0].Data) 85 assert.EqualValues(t, 4, history[1].Offset) 86 assert.Equal(t, "d", history[1].Data) 87 assert.EqualValues(t, 5, history[2].Offset) 88 assert.Equal(t, "e", history[2].Data) 89 }) 90 91 t.Run("With unknown epoch", func(t *testing.T) { 92 history, err := broker.HistoryFrom("test", "unknown", 2) 93 require.Error(t, err) 94 require.Nil(t, history) 95 }) 96 } 97 98 func TestMemory_Store(t *testing.T) { 99 config := NewConfig() 100 101 broker := NewMemoryBroker(nil, &config) 102 broker.SetEpoch("2023") 103 104 ts := time.Now() 105 106 offset, err := broker.Store("test", []byte("a"), 10, ts) 107 require.NoError(t, err) 108 assert.EqualValues(t, 10, offset) 109 110 _, err = broker.Store("test", []byte("b"), 11, ts) 111 require.NoError(t, err) 112 113 _, err = broker.Store("tes2", []byte("c"), 1, ts) 114 require.NoError(t, err) 115 116 history, err := broker.HistoryFrom("test", broker.epoch, 10) 117 require.NoError(t, err) 118 assert.Len(t, history, 1) 119 assert.EqualValues(t, 11, history[0].Offset) 120 121 _, err = broker.Store("test", []byte("c"), 3, ts) 122 assert.Error(t, err) 123 } 124 125 func TestMemstream_filterByOffset(t *testing.T) { 126 ms := &memstream{ 127 ttl: 1, 128 limit: 5, 129 } 130 131 ms.add("test1") 132 ms.add("test2") 133 134 // Should return error if offset is out of range 135 err := ms.filterByOffset(10, func(e *entry) {}) 136 require.Error(t, err) 137 138 err = ms.filterByOffset(1, func(e *entry) { 139 assert.Equal(t, "test2", e.data) 140 }) 141 require.NoError(t, err) 142 143 time.Sleep(2 * time.Second) 144 145 ms.expire() 146 147 err = ms.filterByOffset(1, func(e *entry) { 148 assert.Failf(t, "entry should be expired", "entry: %v", e) 149 }) 150 require.Error(t, err) 151 }