aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-05-31 11:40:28 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-05-31 11:40:28 +0200
commita65a84cfad4eae38e5e84b1ab11f60a62833f287 (patch)
tree3d3b6cc88c188f2c34d3283e081316d6cc05ae83 /tests
parent731556711e453a501f1d1d06a6013b8fbd53414e (diff)
downloadlanes-a65a84cfad4eae38e5e84b1ab11f60a62833f287.tar.gz
lanes-a65a84cfad4eae38e5e84b1ab11f60a62833f287.tar.bz2
lanes-a65a84cfad4eae38e5e84b1ab11f60a62833f287.zip
Improved multi-keeper tests
Diffstat (limited to 'tests')
-rw-r--r--tests/keeper.lua235
-rw-r--r--tests/nb_keepers.lua20
2 files changed, 153 insertions, 102 deletions
diff --git a/tests/keeper.lua b/tests/keeper.lua
index d08dd98..2d432f4 100644
--- a/tests/keeper.lua
+++ b/tests/keeper.lua
@@ -4,99 +4,170 @@
4-- Test program for Lua Lanes 4-- Test program for Lua Lanes
5-- 5--
6-- TODO: there is a random crash when nb_user_keepers > 1. Will have to investigate if it rears its ugly head again 6-- TODO: there is a random crash when nb_user_keepers > 1. Will have to investigate if it rears its ugly head again
7local lanes = require "lanes".configure{ nb_user_keepers = 3, keepers_gc_threshold = 500 } 7-- 3 keepers in addition to the one reserved for the timer linda
8local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{nb_user_keepers = 3, keepers_gc_threshold = 500}
9print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2)
10local lanes = require_lanes_result_1
8 11
9do 12local require_assert_result_1, require_assert_result_2 = require "assert" -- assert.fails()
10 print "Linda names test:" 13print("require_assert_result:", require_assert_result_1, require_assert_result_2)
11 local unnamedLinda = lanes.linda(1) 14
15-- #################################################################################################
16
17local print_id = 0
18local P = function(whence_, ...)
19 print_id = print_id + 1
20 print(whence_, print_id .. ".", ...)
21end
22
23local PRINT = function(...) P("main", ...) end
24
25local DONE = function()
26 PRINT "collecting garbage"
27 collectgarbage()
28 PRINT "SUCCESS\n"
29end
30
31-- #################################################################################################
32-- #################################################################################################
33
34if false then
35 PRINT "========================================================================================="
36 PRINT "Linda groups test:"
37
38 local createLinda = function(...)
39 return lanes.linda(...)
40 end
41
42 -- should succeed
43 assert.failsnot(function() createLinda("zero", 0) end)
44 assert.failsnot(function() createLinda("one", 1) end)
45 assert.failsnot(function() createLinda("two", 2) end)
46 assert.failsnot(function() createLinda("three", 3) end)
47 -- should fail (and not create the lindas)
48 assert.fails(function() createLinda("minus 1", -1) end)
49 assert.fails(function() createLinda("none") end)
50 assert.fails(function() createLinda("four", 4) end)
51
52end
53-- should only collect the 4 successfully created lindas
54DONE()
55
56-- #################################################################################################
57
58if false then
59 PRINT "========================================================================================="
60 PRINT "Linda names test:"
61 local unnamedLinda1 = lanes.linda(1)
12 local unnamedLinda2 = lanes.linda("", 2) 62 local unnamedLinda2 = lanes.linda("", 2)
13 local veeeerrrryyyylooongNamedLinda= lanes.linda( "veeeerrrryyyylooongNamedLinda", 3) 63 local veeeerrrryyyylooongNamedLinda3 = lanes.linda( "veeeerrrryyyylooongNamedLinda", 3)
14 print(unnamedLinda, unnamedLinda2, veeeerrrryyyylooongNamedLinda) 64 assert(tostring(veeeerrrryyyylooongNamedLinda3) == "Linda: veeeerrrryyyylooongNamedLinda")
15 print "GC deadlock test start" 65 local shortNamedLinda0 = lanes.linda( "short", 0)
16 -- store a linda in another linda (-> in a keeper) 66 assert(tostring(shortNamedLinda0) == "Linda: short")
17 unnamedLinda:set("here", lanes.linda("temporary linda", 0)) 67 PRINT(shortNamedLinda0, unnamedLinda1, unnamedLinda2, veeeerrrryyyylooongNamedLinda3)
18 -- repeatedly add and remove stuff in the linda so that a GC happens during the keeper operation 68end
69-- collect the 4 lindas we created
70DONE()
71
72-- #################################################################################################
73
74if true then
75 PRINT "========================================================================================="
76 PRINT "Linda GC test:"
77 local a = lanes.linda("A", 1)
78 local b = lanes.linda("B", 2)
79 local c = lanes.linda("C", 3)
80
81 -- store lindas in each other and in themselves
82 a:set("here", lanes.linda("temporary linda", 0))
83 b:set("here", a, b, c)
84 c:set("here", a, b, c)
85
86 -- repeatedly add and remove stuff in the linda 'a' so that a GC happens during the keeper operation
19 for i = 1, 100 do 87 for i = 1, 100 do
88 io.stdout:write "."
20 for j = 1, 1000 do -- send 1000 tables 89 for j = 1, 1000 do -- send 1000 tables
21 -- print("send #" .. j) 90 -- PRINT("send #" .. j)
22 unnamedLinda:send("here", {"a", "table", "with", "some", "stuff"}) 91 a:send("here", {"a", "table", "with", "some", "stuff"}, j)
23 end 92 end
24 -- print(clearing) 93 -- PRINT(clearing)
25 unnamedLinda:set("here") -- clear everything 94 a:set("here") -- clear everything, including the temporary linda for which we have no reference
26 end 95 end
96 io.stdout:write "\n"
97 b:set("here")
98 c:set("here")
27end 99end
28print "collecting garbage" 100-- should successfully collect a, b, and c and destroy the underlying Deep objects
29collectgarbage() 101DONE()
30print "GC deadlock test done"
31 102
32local print_id = 0 103-- #################################################################################################
33local PRINT = function(...)
34 print_id = print_id + 1
35 print("main", print_id .. ".", ...)
36end
37 104
38local function keeper(linda) 105if false then
39 local mt= { 106 PRINT "========================================================================================="
40 __index= function( _, key ) 107 PRINT "General test:"
41 return linda:get( key )
42 end,
43 __newindex= function( _, key, val )
44 linda:set( key, val )
45 end
46 }
47 return setmetatable( {}, mt )
48end
49 108
50-- 109 local function keeper(linda)
51local lindaA= lanes.linda( "A", 1) 110 local mt= {
52local A= keeper( lindaA ) 111 __index= function( _, key )
53 112 return linda:get( key )
54local lindaB= lanes.linda( "B", 2) 113 end,
55local B= keeper( lindaB ) 114 __newindex= function( _, key, val )
56 115 linda:set( key, val )
57local lindaC= lanes.linda( "C", 3) 116 end
58local C= keeper( lindaC ) 117 }
59print("Created", lindaA, lindaB, lindaC) 118 return setmetatable( {}, mt )
60
61A.some= 1
62PRINT("A.some == " .. A.some )
63assert( A.some==1 )
64
65B.some= "hoo"
66PRINT("B.some == " .. B.some )
67assert( B.some=="hoo" )
68assert( A.some==1 )
69assert( C.some==nil )
70
71function lane()
72 local print_id = 0
73 local PRINT = function(...)
74 print_id = print_id + 1
75 print("lane", print_id .. ".", ...)
76 end 119 end
120
121 --
122 local lindaA= lanes.linda( "A", 1)
123 local A= keeper( lindaA )
124
125 local lindaB= lanes.linda( "B", 2)
126 local B= keeper( lindaB )
127
128 local lindaC= lanes.linda( "C", 3)
129 local C= keeper( lindaC )
130 PRINT("Created", lindaA, lindaB, lindaC)
131
132 A.some= 1
133 PRINT("A.some == " .. A.some )
134 assert( A.some==1 )
135
136 B.some= "hoo"
137 PRINT("B.some == " .. B.some )
138 assert( B.some=="hoo" )
139 assert( A.some==1 )
140 assert( C.some==nil )
141
142 function lane()
143 local PRINT = function(...) P("lane", ...) end
77 144
78 local a= keeper(lindaA) 145 local a= keeper(lindaA)
79 PRINT("a.some == " .. a.some ) 146 PRINT("a.some == " .. a.some )
80 assert( a.some==1 ) 147 assert( a.some==1 )
81 a.some= 2 148 a.some= 2
82 assert( a.some==2 ) 149 assert( a.some==2 )
83 PRINT("a.some == " .. a.some ) 150 PRINT("a.some == " .. a.some )
84
85 local c = keeper(lindaC)
86 assert( c.some==nil )
87 PRINT("c.some == " .. tostring(c.some))
88 c.some= 3
89 assert( c.some==3 )
90 PRINT("c.some == " .. c.some)
91end
92 151
93PRINT("lane started") 152 local c = keeper(lindaC)
94local h= lanes.gen( "io", lane )() 153 assert( c.some==nil )
95PRINT("lane joined:", h:join()) 154 PRINT("c.some == " .. tostring(c.some))
155 c.some= 3
156 assert( c.some==3 )
157 PRINT("c.some == " .. c.some)
158 end
159
160 PRINT("lane started")
161 local h= lanes.gen( "io", lane )()
162 PRINT("lane joined:", h:join())
163
164 PRINT("A.some = " .. A.some )
165 assert( A.some==2 )
166 PRINT("C.some = " .. C.some )
167 assert( C.some==3 )
168 lindaC:set("some")
169 assert( C.some==nil )
170end
171DONE()
96 172
97PRINT("A.some = " .. A.some ) 173print "\nTEST OK" \ No newline at end of file
98assert( A.some==2 )
99PRINT("C.some = " .. C.some )
100assert( C.some==3 )
101lindaC:set("some")
102assert( C.some==nil )
diff --git a/tests/nb_keepers.lua b/tests/nb_keepers.lua
deleted file mode 100644
index 575138c..0000000
--- a/tests/nb_keepers.lua
+++ /dev/null
@@ -1,20 +0,0 @@
1-- 2 keepers in addition to the one reserved for the timer linda
2local require_lanes_result_1, require_lanes_result_2 = require "lanes".configure{nb_user_keepers = 2}
3print("require_lanes_result:", require_lanes_result_1, require_lanes_result_2)
4local lanes = require_lanes_result_1
5
6local require_assert_result_1, require_assert_result_2 = require "assert" -- assert.fails()
7print("require_assert_result:", require_assert_result_1, require_assert_result_2)
8
9local createLinda = function(...)
10 return lanes.linda(...)
11end
12
13-- should succeed
14assert.failsnot(function() createLinda("one", 1) end)
15assert.failsnot(function() createLinda("two", 2) end)
16-- should fail
17assert.fails(function() createLinda("none") end)
18assert.fails(function() createLinda("zero", 0) end)
19assert.fails(function() createLinda("three", 3) end)
20print "TEST OK"