forked from sonic-net/sonic-swss-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_redis_ut.py
124 lines (114 loc) · 3.81 KB
/
test_redis_ut.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import time
from threading import Thread
from pympler.tracker import SummaryTracker
from swsscommon import swsscommon
def test_ProducerTable():
db = swsscommon.DBConnector("APPL_DB", 0, True)
ps = swsscommon.ProducerTable(db, "abc")
cs = swsscommon.ConsumerTable(db, "abc")
fvs = swsscommon.FieldValuePairs([('a','b')])
ps.set("bbb", fvs)
(key, op, cfvs) = cs.pop()
assert key == "bbb"
assert op == "SET"
assert len(cfvs) == 1
assert cfvs[0] == ('a', 'b')
def test_ProducerStateTable():
db = swsscommon.DBConnector("APPL_DB", 0, True)
ps = swsscommon.ProducerStateTable(db, "abc")
cs = swsscommon.ConsumerStateTable(db, "abc")
fvs = swsscommon.FieldValuePairs([('a','b')])
ps.set("aaa", fvs)
(key, op, cfvs) = cs.pop()
assert key == "aaa"
assert op == "SET"
assert len(cfvs) == 1
assert cfvs[0] == ('a', 'b')
def test_Table():
db = swsscommon.DBConnector("APPL_DB", 0, True)
tbl = swsscommon.Table(db, "test_TABLE")
fvs = swsscommon.FieldValuePairs([('a','b'), ('c', 'd')])
tbl.set("aaa", fvs)
keys = tbl.getKeys()
assert len(keys) == 1
assert keys[0] == "aaa"
(status, fvs) = tbl.get("aaa")
assert status == True
assert len(fvs) == 2
assert fvs[0] == ('a', 'b')
assert fvs[1] == ('c', 'd')
def test_SubscriberStateTable():
db = swsscommon.DBConnector("APPL_DB", 0, True)
t = swsscommon.Table(db, "testsst")
sel = swsscommon.Select()
cst = swsscommon.SubscriberStateTable(db, "testsst")
sel.addSelectable(cst)
fvs = swsscommon.FieldValuePairs([('a','b')])
t.set("aaa", fvs)
(state, c) = sel.select()
assert state == swsscommon.Select.OBJECT
(key, op, cfvs) = cst.pop()
assert key == "aaa"
assert op == "SET"
assert len(cfvs) == 1
assert cfvs[0] == ('a', 'b')
def test_Notification():
db = swsscommon.DBConnector("APPL_DB", 0, True)
ntfc = swsscommon.NotificationConsumer(db, "testntf")
sel = swsscommon.Select()
sel.addSelectable(ntfc)
fvs = swsscommon.FieldValuePairs([('a','b')])
ntfp = swsscommon.NotificationProducer(db, "testntf")
ntfp.send("aaa", "bbb", fvs)
(state, c) = sel.select()
assert state == swsscommon.Select.OBJECT
(op, data, cfvs) = ntfc.pop()
assert op == "aaa"
assert data == "bbb"
assert len(cfvs) == 1
assert cfvs[0] == ('a', 'b')
def test_DBConnectorRedisClientName():
db = swsscommon.DBConnector("APPL_DB", 0, True)
time.sleep(1)
assert db.getClientName() == ""
client_name = "foo"
db.setClientName(client_name)
time.sleep(1)
assert db.getClientName() == client_name
client_name = "bar"
db.setClientName(client_name)
time.sleep(1)
assert db.getClientName() == client_name
client_name = "foobar"
db.setClientName(client_name)
time.sleep(1)
assert db.getClientName() == client_name
def test_SelectMemoryLeak():
N = 50000
def table_set(t, state):
fvs = swsscommon.FieldValuePairs([("status", state)])
t.set("123", fvs)
def generator_SelectMemoryLeak():
app_db = swsscommon.DBConnector("APPL_DB", 0, True)
t = swsscommon.Table(app_db, "TABLE")
for i in range(int(N/2)):
table_set(t, "up")
table_set(t, "down")
tracker = SummaryTracker()
appl_db = swsscommon.DBConnector("APPL_DB", 0, True)
sel = swsscommon.Select()
sst = swsscommon.SubscriberStateTable(appl_db, "TABLE")
sel.addSelectable(sst)
thr = Thread(target=generator_SelectMemoryLeak)
thr.daemon = True
thr.start()
time.sleep(5)
for _ in range(N):
state, c = sel.select(1000)
diff = tracker.diff()
cases = []
for name, count, _ in diff:
if count >= N:
cases.append("%s - %d objects for %d repeats" % (name, count, N))
thr.join()
assert not cases