Skip to content

Commit 33b1fe5

Browse files
setup coroutine test
1 parent 950a43c commit 33b1fe5

File tree

3 files changed

+129
-67
lines changed

3 files changed

+129
-67
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"aos:publish": "cross-env NODE_OPTIONS=\"--import=./register.js \" node tools/bundle-aos.ts && node tools/publish-aos.ts",
1010
"aos:load": "cross-env NODE_OPTIONS=\"--import=./register.js \" node tools/bundle-aos.ts && node tools/load-aos.ts",
1111
"aos:spawn": "cross-env NODE_OPTIONS=\"--import=./register.js \" node tools/spawn-aos.ts",
12-
"test": "cross-env NODE_OPTIONS=\"--import=./register.js \" node --test-concurrency 1 --experimental-wasm-memory64 **/*.test.ts"
12+
"test": "cross-env NODE_OPTIONS=\"--import=./register.js \" node --test-concurrency 1 --experimental-wasm-memory64 **/*.test.ts",
13+
"test:e2e": "cross-env NODE_OPTIONS=\"--import=./register.js \" node ./test/e2e/coroutine-scale.ts"
1314
},
1415
"devDependencies": {
1516
"@permaweb/ao-loader": "^0.0.35",

src/common/main.lua

+82-66
Original file line numberDiff line numberDiff line change
@@ -49,82 +49,98 @@ main.init = function()
4949
error(registerRes)
5050
end
5151

52-
ao.send({
52+
print("Message reference: " .. msg.Nonce)
53+
local startTime = os.time()
54+
local stateMsg = ao.send({
5355
Target = antId,
5456
Action = "State",
55-
})
57+
}).receive(Handlers.utils.hasMatchingTag("Action", ActionMap.StateNotice), function(msg)
58+
return msg
59+
end)
60+
print(
61+
"Time taken: "
62+
.. os.time() - startTime
63+
.. " "
64+
.. "Message reference: "
65+
.. msg.Nonce
66+
.. ":"
67+
.. stateMsg.Nonce
68+
)
69+
5670
ao.send({
5771
Target = msg.From,
5872
Action = "Register-Notice",
5973
})
60-
end)
61-
)
6274

63-
Handlers.add(
64-
camel(ActionMap.StateNotice),
65-
Handlers.utils.hasMatchingTag("Action", ActionMap.StateNotice),
66-
ANT_DB_ADMIN:createSafeTransaction(function(msg)
67-
print("Action: " .. ActionMap.StateNotice)
68-
local stateStatus, stateRes = pcall(utils.parseAntState, msg.Data)
69-
if not stateStatus then
70-
ao.send({
71-
Target = msg.From,
72-
Action = "State-Notice-Failure",
73-
["Message-Id"] = msg.Id,
74-
Data = tostring(stateRes),
75-
})
76-
error(stateRes)
77-
end
78-
79-
-- Check if already registered and send notice of registration if not registered
80-
local isRegisteredStatus, isRegisteredRes = pcall(ANT_DB_ADMIN.isRegistered, ANT_DB_ADMIN, msg.From)
81-
if not isRegisteredStatus then
82-
ao.send({
83-
Target = msg.From,
84-
Action = "State-Notice-Failure",
85-
["Message-Id"] = msg.Id,
86-
Data = isRegisteredRes,
87-
})
88-
error(isRegisteredRes)
89-
end
90-
-- Register the ANT and send a notice if it is not already registered
91-
if isRegisteredRes == false then
92-
local registrationStatus, registrationRes =
93-
pcall(ANT_DB_ADMIN.register, ANT_DB_ADMIN, msg.From, tonumber(msg.Timestamp))
94-
if not registrationStatus then
95-
ao.send({
96-
Target = msg.From,
97-
Action = "Register-Notice-Failure",
98-
["Message-Id"] = msg.Id,
99-
Data = registrationRes,
100-
})
101-
error(registrationRes)
102-
end
103-
end
104-
105-
local aclUpdateStatus, aclUpdateRes = pcall(ANT_DB_ADMIN.updateACL, ANT_DB_ADMIN, msg.From, stateRes)
106-
107-
if not aclUpdateStatus then
108-
ao.send({
109-
Target = msg.From,
110-
Action = "State-Notice-Failure",
111-
["Message-Id"] = msg.Id,
112-
Data = aclUpdateRes,
113-
})
114-
error(aclUpdateRes)
115-
end
116-
117-
-- notify the ant that it has been registered in this action
118-
if isRegisteredRes == false then
119-
ao.send({
120-
Target = msg.From,
121-
Action = "Register-Notice",
122-
["Message-Id"] = msg.Id,
123-
})
124-
end
75+
return "I have returned"
12576
end)
12677
)
12778

79+
-- Handlers.add(
80+
-- camel(ActionMap.StateNotice),
81+
-- Handlers.utils.hasMatchingTag("Action", ActionMap.StateNotice),
82+
-- ANT_DB_ADMIN:createSafeTransaction(function(msg)
83+
-- print("Action: " .. ActionMap.StateNotice)
84+
-- local stateStatus, stateRes = pcall(utils.parseAntState, msg.Data)
85+
-- if not stateStatus then
86+
-- ao.send({
87+
-- Target = msg.From,
88+
-- Action = "State-Notice-Failure",
89+
-- ["Message-Id"] = msg.Id,
90+
-- Data = tostring(stateRes),
91+
-- })
92+
-- error(stateRes)
93+
-- end
94+
95+
-- -- Check if already registered and send notice of registration if not registered
96+
-- local isRegisteredStatus, isRegisteredRes = pcall(ANT_DB_ADMIN.isRegistered, ANT_DB_ADMIN, msg.From)
97+
-- if not isRegisteredStatus then
98+
-- ao.send({
99+
-- Target = msg.From,
100+
-- Action = "State-Notice-Failure",
101+
-- ["Message-Id"] = msg.Id,
102+
-- Data = isRegisteredRes,
103+
-- })
104+
-- error(isRegisteredRes)
105+
-- end
106+
-- -- Register the ANT and send a notice if it is not already registered
107+
-- if isRegisteredRes == false then
108+
-- local registrationStatus, registrationRes =
109+
-- pcall(ANT_DB_ADMIN.register, ANT_DB_ADMIN, msg.From, tonumber(msg.Timestamp))
110+
-- if not registrationStatus then
111+
-- ao.send({
112+
-- Target = msg.From,
113+
-- Action = "Register-Notice-Failure",
114+
-- ["Message-Id"] = msg.Id,
115+
-- Data = registrationRes,
116+
-- })
117+
-- error(registrationRes)
118+
-- end
119+
-- end
120+
121+
-- local aclUpdateStatus, aclUpdateRes = pcall(ANT_DB_ADMIN.updateACL, ANT_DB_ADMIN, msg.From, stateRes)
122+
123+
-- if not aclUpdateStatus then
124+
-- ao.send({
125+
-- Target = msg.From,
126+
-- Action = "State-Notice-Failure",
127+
-- ["Message-Id"] = msg.Id,
128+
-- Data = aclUpdateRes,
129+
-- })
130+
-- error(aclUpdateRes)
131+
-- end
132+
133+
-- -- notify the ant that it has been registered in this action
134+
-- if isRegisteredRes == false then
135+
-- ao.send({
136+
-- Target = msg.From,
137+
-- Action = "Register-Notice",
138+
-- ["Message-Id"] = msg.Id,
139+
-- })
140+
-- end
141+
-- end)
142+
-- )
143+
128144
Handlers.add(
129145
camel(ActionMap.AccessControlList),
130146
Handlers.utils.hasMatchingTag("Action", ActionMap.AccessControlList),

test/e2e/coroutine-scale.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { connect, createDataItemSigner } from '@permaweb/aoconnect';
2+
import Arweave from 'arweave';
3+
4+
const ao = connect();
5+
const arweave = Arweave.init({});
6+
async function main() {
7+
const processId = 'AFo0NmQdBVRaTm0bv6u6EHqryXam_gtNc9z1EYHeFQs';
8+
const antId = 'Y7HFoTJTBckigX4_4N0IOxAMkD_VMKIopnIjSyq2qhI';
9+
10+
const msgQty = 4;
11+
const jwk = await arweave.wallets.generate();
12+
const signer = createDataItemSigner(jwk);
13+
14+
async function sleep(ms: number) {
15+
return new Promise((resolve) => setTimeout(resolve, ms));
16+
}
17+
for (const i of new Array(msgQty).fill(0)) {
18+
console.log('Registering', i);
19+
ao.message({
20+
process: processId,
21+
tags: [
22+
{ name: 'Action', value: 'Register' },
23+
{ name: 'Process-Id', value: antId },
24+
],
25+
signer,
26+
});
27+
28+
await sleep(3_000);
29+
}
30+
31+
// await Promise.all(
32+
// new Array(msgQty).fill(0).map((_, i) => {
33+
// ao.message({
34+
// process: processId,
35+
// tags: [
36+
// { name: 'Action', value: 'Register' },
37+
// { name: 'Process-Id', value: antId },
38+
// ],
39+
// signer,
40+
// });
41+
// }),
42+
// );
43+
}
44+
45+
main();

0 commit comments

Comments
 (0)