-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_behind_test.go
40 lines (30 loc) · 1003 Bytes
/
write_behind_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package multi_tier_caching
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestWriteQueue(t *testing.T) {
var processedTasks []WriteTask
var mu sync.Mutex
processor := func(task WriteTask) {
mu.Lock()
processedTasks = append(processedTasks, task)
mu.Unlock()
}
wq := NewWriteQueue(processor, false)
task1 := WriteTask{Key: "key1", Value: "value1"}
task2 := WriteTask{Key: "key2", Value: "value2"}
wq.Enqueue(task1)
wq.Enqueue(task2)
// Wait for the queue to process tasks
time.Sleep(2 * time.Second)
mu.Lock()
defer mu.Unlock()
assert.Len(t, processedTasks, 2, "There are 2 tasks to be processed")
assert.Equal(t, "key1", processedTasks[0].Key, "The first task must be key1")
assert.Equal(t, "value1", processedTasks[0].Value, "The first task must contain value1")
assert.Equal(t, "key2", processedTasks[1].Key, "The second task must be key2")
assert.Equal(t, "value2", processedTasks[1].Value, "The second task must contain value2")
}