Skip to content

Commit c2d2e7a

Browse files
committed
prepare client test
1 parent 604d6a6 commit c2d2e7a

File tree

9 files changed

+213
-17
lines changed

9 files changed

+213
-17
lines changed

.luarc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"version": "Lua 5.4",
2121
"path": [
2222
"script/?.lua",
23-
"script/?/init.lua"
23+
"script/?/init.lua",
24+
"test/?.lua",
25+
"test/?/init.lua"
2426
],
2527
"pathStrict": true
2628
},

script/files.lua

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@ local encoder = require 'encoder'
1616
---@class files
1717
local m = {}
1818

19-
m.openMap = {}
20-
m.libraryMap = {}
21-
m.fileMap = {}
22-
m.dllMap = {}
2319
m.watchList = {}
2420
m.notifyCache = {}
25-
m.visible = {}
2621
m.assocVersion = -1
2722
m.assocMatcher = nil
28-
m.globalVersion = 0
29-
m.fileCount = 0
30-
m.astCount = 0
31-
m.astMap = {} -- setmetatable({}, { __mode = 'v' })
23+
24+
function m.reset()
25+
m.openMap = {}
26+
m.libraryMap = {}
27+
m.fileMap = {}
28+
m.dllMap = {}
29+
m.visible = {}
30+
m.globalVersion = 0
31+
m.fileCount = 0
32+
m.astCount = 0
33+
m.astMap = {}
34+
end
35+
36+
m.reset()
3237

3338
local fixedUri = {}
3439
--- 获取文件的真实uri(真实大小写)

script/gc.lua

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
local util = require 'share.utility'
2+
3+
---@class gc
4+
---@field _list table
5+
local mt = {}
6+
mt.__index = mt
7+
mt.type = 'gc'
8+
9+
mt._max = 10
10+
11+
local function destroyGCObject(obj)
12+
local tp = type(obj)
13+
if tp == 'function' then
14+
xpcall(obj, log.error)
15+
elseif tp == 'table' then
16+
local remove = obj.remove
17+
if type(remove) == 'function' then
18+
xpcall(remove, log.error, obj)
19+
end
20+
end
21+
end
22+
23+
local function isRemoved(obj)
24+
local tp = type(obj)
25+
if tp == 'function' then
26+
for i = 1, 1000 do
27+
local n, v = debug.getupvalue(obj, i)
28+
if not n then
29+
log.warn('函数式析构器没有 removed 上值!', util.dump(debug.getinfo(obj)))
30+
break
31+
end
32+
if n == 'removed' then
33+
if v then
34+
return true
35+
end
36+
break
37+
end
38+
end
39+
elseif tp == 'table' then
40+
if obj._removed then
41+
return true
42+
end
43+
end
44+
return false
45+
end
46+
47+
local function zip(self)
48+
local list = self._list
49+
local index = 1
50+
for i = 1, #list do
51+
local obj = list[index]
52+
if not obj then
53+
break
54+
end
55+
if isRemoved(obj) then
56+
if index == #list then
57+
list[#list] = nil
58+
break
59+
end
60+
list[index] = list[#list]
61+
else
62+
index = index + 1
63+
end
64+
end
65+
self._max = #list * 1.5
66+
if self._max < 10 then
67+
self._max = 10
68+
end
69+
end
70+
71+
function mt:remove()
72+
if self._removed then
73+
return
74+
end
75+
self._removed = true
76+
local list = self._list
77+
for i = 1, #list do
78+
destroyGCObject(list[i])
79+
end
80+
end
81+
82+
--- 标记`obj`在buff移除时自动移除。如果`obj`是个`function`,
83+
--- 则直接调用;如果`obj`是个`table`,则调用内部的`remove`方法。
84+
--- 其他情况不做处理
85+
---@param obj any
86+
---@return any
87+
function mt:add(obj)
88+
if self._removed then
89+
destroyGCObject(obj)
90+
return nil
91+
end
92+
self._list[#self._list+1] = obj
93+
if #self._list > self._max then
94+
zip(self)
95+
end
96+
return obj
97+
end
98+
99+
--- 创建一个gc容器,使用 `gc:add(obj)` 将析构器放入gc容器。
100+
---
101+
--- 当gc容器被销毁时,会调用内部的析构器(不保证调用顺序)
102+
---
103+
--- 析构器必须是以下格式中的一种:
104+
--- 1. 一个对象,使用 `obj:remove()` 方法来析构,使用 `obj._removed` 属性来标记已被析构。
105+
--- 2. 一个析构函数,使用上值 `removed` 来标记已被析构。
106+
---
107+
--- ```lua
108+
--- local gc = ac.gc() -- 创建gc容器
109+
--- gc:add(obj1) -- 将obj1放入gc容器
110+
--- gc:add(obj2) -- 将obj2放入gc容器
111+
--- gc:remove() -- 移除gc容器,同时也会移除obj1与obj2
112+
--- ```
113+
return function ()
114+
return setmetatable({
115+
_list = {},
116+
}, mt)
117+
end

script/workspace/scope.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ end
7676
---@class scope.manager
7777
local m = {}
7878

79-
---@type scope[]
80-
m.folders = {}
81-
m.override = createScope 'override'
82-
m.fallback = createScope 'fallback'
79+
function m.reset()
80+
---@type scope[]
81+
m.folders = {}
82+
m.override = createScope 'override'
83+
m.fallback = createScope 'fallback'
84+
end
85+
86+
m.reset()
8387

8488
---@param uri uri
8589
---@return scope

script/workspace/workspace.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ local loading = require 'workspace.loading'
1616
---@class workspace
1717
local m = {}
1818
m.type = 'workspace'
19-
---@type scope[]
20-
m.folders = {}
2119
m.watchList = {}
2220

2321
--- 注册事件
@@ -53,6 +51,12 @@ function m.create(uri)
5351
m.folders[#m.folders+1] = scp
5452
end
5553

54+
function m.reset()
55+
---@type scope[]
56+
m.folders = {}
57+
m.rootUri = nil
58+
end
59+
5660
function m.getRootUri(uri)
5761
local scp = m.getScope(uri)
5862
return scp.uri

test.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ local function main()
9999
require 'bee.platform'.OS = 'macOS'
100100
testAll()
101101

102-
test 'full'
102+
test 'client'
103+
104+
--test 'full'
103105

104106
print('测试完成')
105107
end

test/client/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'client.tests.single-mode'

test/client/lclient.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
local gc = require 'gc'
2+
local util = require 'utility'
3+
4+
---@class languageClient
5+
---@field _inFile file*
6+
---@field _outFile file*
7+
---@field _gc gc
8+
local mt = {}
9+
mt.__index = mt
10+
11+
function mt:__close()
12+
self:remove()
13+
end
14+
15+
function mt:_openIO()
16+
local r = math.random(1, 10000)
17+
self._inPath = ('%s/%s.in'):format(LOGPATH, r)
18+
self._outPath = ('%s/%s.out'):format(LOGPATH, r)
19+
local stdin = io.open(self._inPath, 'rb')
20+
local stdout = io.open(self._outPath, 'wb')
21+
self._inFile = io.open(self._inPath, 'wb')
22+
self._outFile = io.open(self._outPath, 'rb')
23+
io.stdin = stdin
24+
io.stdout = stdout
25+
self:gc(function ()
26+
stdin:close()
27+
stdout:close()
28+
self._inFile:close()
29+
self._outFile:close()
30+
end)
31+
end
32+
33+
function mt:_flushServer()
34+
-- reset scopes
35+
local ws = require 'workspace'
36+
local scope = require 'workspace.scope'
37+
local files = require 'files'
38+
ws.reset()
39+
scope.reset()
40+
files.reset()
41+
end
42+
43+
function mt:start()
44+
self:_openIO()
45+
self:_flushServer()
46+
end
47+
48+
function mt:gc(obj)
49+
return self._gc:add(obj)
50+
end
51+
52+
function mt:remove()
53+
self._gc:remove()
54+
end
55+
56+
return function ()
57+
local self = setmetatable({
58+
_gc = gc(),
59+
}, mt)
60+
return self
61+
end

test/client/tests/single-mode.lua

Whitespace-only changes.

0 commit comments

Comments
 (0)