-
Notifications
You must be signed in to change notification settings - Fork 9
/
meta_test.go
97 lines (77 loc) · 1.92 KB
/
meta_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package sfu
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestMetadata(t *testing.T) {
type dataValue struct {
key string
value interface{}
}
// Test NewMetadata function
m := NewMetadata()
if m == nil {
t.Error("NewMetadata returned nil")
}
_, err := m.Get("key1")
require.Equal(t, ErrMetaNotFound, err)
reqData := map[string]interface{}{
"key1": "data1",
"key2": "data2",
"key3": "data3",
"key4": "data4",
"key5": "data5",
"key6": "data6",
"key7": "data7",
"key8": "data8",
"key9": "data9",
"key10": "data10",
}
// Test OnChanged method
receivedMetas := make(map[string]interface{})
chanMeta := make(chan dataValue, len(reqData))
callback1 := m.OnChanged(func(key string, value interface{}) {
t.Logf("Key: %s, Value: %v", key, value)
chanMeta <- dataValue{key: key, value: value}
})
// Test Set and Get methods
for k, v := range reqData {
m.Set(k, v)
if value, _ := m.Get(k); value != v {
t.Errorf("Get returned %v, expected %v", value, "value1")
}
}
for i := 0; i < len(reqData); i++ {
select {
case data := <-chanMeta:
receivedMetas[data.key] = data.value
case <-time.After(100 * time.Millisecond):
t.Error("timeout waiting for meta")
}
}
require.Equal(t, len(reqData), len(receivedMetas))
// Test ForEach method
m.ForEach(func(key string, value interface{}) {
t.Logf("Key: %s, Value: %v", key, value)
})
// Test Delete method
for k := range reqData {
_ = m.Delete(k)
if _, err := m.Get(k); err != ErrMetaNotFound {
t.Errorf("Get returned %v, expected %v", err, ErrMetaNotFound)
}
}
// cancel the listener above
callback1.Remove()
close(chanMeta)
// Test OnChanged method with cancel
var state = true
callback2 := m.OnChanged(func(key string, value interface{}) {
t.Logf("Key: %s, Value: %v", key, value)
state = false
})
callback2.Remove()
m.Set("key6", "value6")
require.Equal(t, true, state)
}