Concurrency.lua 7.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
local _G = require "_G"
local assert = _G.assert
local error = _G.error
local ipairs = _G.ipairs
local pairs = _G.pairs
local pcall = _G.pcall
local type = _G.type
local coroutine = require "coroutine"
local string = require "string"
local io = require "io"
local uuid = require "uuid"
local giop = require "oil.corba.giop"
local cothread = require "cothread"
local openbus = require "openbus"
15
local libidl = require "openbus.idl"
16
17
18
19
20
21
22
local idl = require "openbus.core.idl"
local msg = require "openbus.util.messages"
local log = require "openbus.util.logger"

local sysex = giop.SystemExceptionIDs

bushost, busport, verbose = ...
23
require "openbus.test.configs"
24
25

local smalltime = .1
26
local connprops = { accesskey = openbus.newKey() }
27
28
29

-- login as admin and provide additional functionality for the test
local invalidate, shutdown do
30
31
  local orb = openbus.initORB()
  local OpenBusContext = orb.OpenBusContext
32
  local conn = OpenBusContext:createConnection(bushost, busport)
33
  conn:loginByPassword(admin, admpsw)
34
  OpenBusContext:setDefaultConnection(conn)
35
  function invalidate(loginId)
36
    OpenBusContext:getLoginRegistry():invalidateLogin(loginId)
37
38
39
  end
  function shutdown()
    conn:logout()
40
    orb:shutdown()
41
42
43
44
  end
end

local orb = openbus.initORB()
45
46
local OpenBusContext = orb.OpenBusContext
assert(OpenBusContext.orb == orb)
47

48
49
50
51
52
53
54
55
56
57
58
do log:TEST("Make changes on LRU max cache size")
  local default = require("loop.collection.LRUCache").maxsize
  local conn = OpenBusContext:createConnection(bushost, busport, connprops)
  assert(conn:maxCacheSize() == default)
  conn:maxCacheSize(1024)
  assert(conn:maxCacheSize() == 1024)
  -- TODO: exercise more calls than default initial value could perform
  conn:maxCacheSize(default)
  assert(conn:maxCacheSize() == default)
  conn:logout()
end
59
60

do log:TEST("Two threads logging in")
61
  local conn = OpenBusContext:createConnection(bushost, busport, connprops)
62
63
64
65
66
67
68
  local failures = 0
  local threads = 2
  local function trylogin()
    local ok, ex = pcall(conn.loginByPassword, conn, user, password)
    threads = threads-1
    if not ok then
      failures = failures+1
69
      assert(ex._repid == libidl.types.AlreadyLoggedIn)
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    end
  end
  
  for i = 1, threads do
    cothread.schedule(coroutine.create(trylogin))
  end
  repeat cothread.delay(smalltime) until threads == 0
  
  assert(conn.login ~= nil)
  assert(uuid.isvalid(conn.login.id))
  assert(conn.login.entity == user)
  assert(failures == 1)
  
  conn:logout()
end

do log:TEST("Two threads getting invalid login")
87
  local conn = OpenBusContext:createConnection(bushost, busport, connprops)
88
  conn:loginByPassword(user, password)
89
90
  OpenBusContext:setDefaultConnection(conn)
  local OfferRegistry = OpenBusContext:getOfferRegistry()
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  
  local suspended = {}
  local mylogin = conn.login
  function conn:onInvalidLogin(login)
    assert(self == conn)
    assert(self.login == nil)
    assert(login == mylogin)
    suspended[#suspended+1] = cothread.running()
    cothread.suspend()
  end
  
  local failures = 0
  local threads = 2
  local function callop()
105
    local ok, ex = pcall(OfferRegistry.getAllServices, OfferRegistry)
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    threads = threads-1
    if not ok then
      failures = failures+1
      assert(ex._repid == sysex.NO_PERMISSION)
      assert(ex.completed == "COMPLETED_NO")
      assert(ex.minor == idl.const.services.access_control.NoLoginCode)
    end
  end
  
  -- invalidate login
  invalidate(mylogin.id)
  
  -- perform calls with an invalid login
  for i = 1, threads do cothread.schedule(coroutine.create(callop)) end
  repeat cothread.delay(smalltime) until #suspended == threads
  
  -- check connection behavion while logged wiht invalid login
  assert(conn.login == nil)
  local ok, ex = pcall(conn.loginByPassword, conn, user, "ThisIsNot:"..password)
  assert(not ok)
  assert(ex._repid == idl.types.services.access_control.AccessDenied)
  assert(conn.login == nil)
  
  for _, thread in ipairs(suspended) do cothread.schedule(thread) end
  repeat cothread.delay(smalltime) until threads == 0
  
  -- check connection after execution of callbacks
  assert(failures == 2)
  assert(conn.login == nil)
  
136
  OpenBusContext:setDefaultConnection(nil)
137
138
139
end

do log:TEST("Two threads getting invalid login while other relogs")
140
  local conn = OpenBusContext:createConnection(bushost, busport, connprops)
141
  conn:loginByPassword(user, password)
142
143
  OpenBusContext:setDefaultConnection(conn)
  local OfferRegistry = OpenBusContext:getOfferRegistry()
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  
  local suspended = {}
  local mylogin = conn.login
  function conn:onInvalidLogin(login)
    assert(self == conn)
    assert(self.login == nil)
    assert(login == mylogin)
    suspended[#suspended+1] = cothread.running()
    cothread.suspend()
  end
  
  local failures = 0
  local threads = 2
  local function callop()
158
    OfferRegistry:getAllServices()
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    threads = threads-1
  end
  
  -- invalidate login
  invalidate(mylogin.id)
  
  -- perform calls with an invalid login
  for i = 1, threads do cothread.schedule(coroutine.create(callop)) end
  repeat cothread.delay(smalltime) until #suspended == threads
  
  -- check connection behavion while logged wiht invalid login
  assert(conn.login == nil)
  conn:loginByPassword(user, password)
  assert(conn.login ~= nil)
  assert(conn.login ~= mylogin)
  
  -- resume threads calling 'onInvalidLogin' callback
  for _, thread in ipairs(suspended) do cothread.schedule(thread) end
  repeat cothread.delay(smalltime) until threads == 0
  
  -- check connection after execution of callbacks
  assert(conn.login ~= nil)
  assert(conn.login ~= mylogin)
  
  conn:logout()
184
  OpenBusContext:setDefaultConnection(nil)
185
186
187
end

do log:TEST("Two threads getting invalid login and trying to relog")
188
  local conn = OpenBusContext:createConnection(bushost, busport, connprops)
189
  conn:loginByPassword(user, password)
190
191
  OpenBusContext:setDefaultConnection(conn)
  local OfferRegistry = OpenBusContext:getOfferRegistry()
192
193
194
195
196
197
198
199
200
201
202
203
204
  
  local suspended = {}
  local mylogin = conn.login
  local alreadylogged = 0
  function conn:onInvalidLogin(login)
    assert(self == conn)
    assert(self.login == nil)
    assert(login == mylogin)
    suspended[#suspended+1] = cothread.running()
    cothread.suspend()
    local ok, ex = pcall(conn.loginByPassword, conn, user, password)
    if not ok then
      alreadylogged = alreadylogged+1
205
      assert(ex._repid == libidl.types.AlreadyLoggedIn)
206
207
208
209
210
211
    end
  end
  
  local failures = 0
  local threads = 2
  local function callop()
212
    local ok, ex = pcall(OfferRegistry.getAllServices, OfferRegistry)
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    threads = threads-1
    if not ok then
      failures = failures+1
      assert(ex._repid == sysex.NO_PERMISSION)
      assert(ex.completed == "COMPLETED_NO")
      assert(ex.minor == idl.const.services.access_control.NoLoginCode)
    end
  end
  
  -- invalidate login
  invalidate(mylogin.id)
  
  -- perform calls with an invalid login
  for i = 1, threads do cothread.schedule(coroutine.create(callop)) end
  repeat cothread.delay(smalltime) until #suspended == threads
  
  -- check connection behavion while logged wiht invalid login
  assert(conn.login == nil)
  
  -- resume threads calling 'onInvalidLogin' callback
  for _, thread in ipairs(suspended) do cothread.schedule(thread) end
  repeat cothread.delay(smalltime) until threads == 0
  
  -- check connection after execution of callbacks
  assert(failures == 0)
  assert(alreadylogged == 1)
  assert(conn.login ~= nil)
  assert(conn.login ~= mylogin)
  
  conn:logout()
243
  OpenBusContext:setDefaultConnection(nil)
244
245
end

246
orb:shutdown()
247
shutdown()