mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
rewrite makefile, and remove luacode cache.
This commit is contained in:
12
test/pingqueue.lua
Normal file
12
test/pingqueue.lua
Normal file
@@ -0,0 +1,12 @@
|
||||
local skynet = require "skynet"
|
||||
local mqueue = require "mqueue"
|
||||
|
||||
skynet.start(function()
|
||||
local id = 0
|
||||
local pingserver = skynet.newservice "pingserver"
|
||||
mqueue.register(function(str)
|
||||
id = id + 1
|
||||
str = string.format("id = %d , %s",id, str)
|
||||
return skynet.call(pingserver, "lua", "PING", str)
|
||||
end)
|
||||
end)
|
||||
26
test/pingserver.lua
Normal file
26
test/pingserver.lua
Normal file
@@ -0,0 +1,26 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
local command = {}
|
||||
|
||||
function command.PING(hello)
|
||||
skynet.ret(skynet.pack(hello))
|
||||
end
|
||||
|
||||
function command.HELLO()
|
||||
skynet.sleep(100)
|
||||
skynet.ret(skynet.pack("hello"))
|
||||
end
|
||||
|
||||
function command.EXIT()
|
||||
skynet.exit()
|
||||
end
|
||||
|
||||
function command.ERROR()
|
||||
error "throw an error"
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("lua", function(session,addr, cmd, ...)
|
||||
command[cmd](...)
|
||||
end)
|
||||
end)
|
||||
10
test/testblockcall.lua
Normal file
10
test/testblockcall.lua
Normal file
@@ -0,0 +1,10 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
local ping = skynet.newservice("pingserver")
|
||||
skynet.timeout(0,function()
|
||||
print(skynet.call(ping,"lua","PING","ping"))
|
||||
end)
|
||||
|
||||
print(skynet.blockcall(ping,"lua","HELLO"))
|
||||
end)
|
||||
9
test/testdb.lua
Normal file
9
test/testdb.lua
Normal file
@@ -0,0 +1,9 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
local result = skynet.call("SIMPLEDB","text","SET","foobar","hello")
|
||||
print(result)
|
||||
result = skynet.call("SIMPLEDB","text","GET","foobar")
|
||||
print(result)
|
||||
skynet.exit()
|
||||
end)
|
||||
19
test/testgroup.lua
Normal file
19
test/testgroup.lua
Normal file
@@ -0,0 +1,19 @@
|
||||
local skynet = require "skynet"
|
||||
local group = require "mcgroup"
|
||||
--local group = require "localgroup"
|
||||
|
||||
skynet.start(function()
|
||||
local gid = group.create()
|
||||
local gaddr = group.address(gid)
|
||||
local g = {}
|
||||
print("=== Create Group ===",gid,skynet.address(gaddr))
|
||||
for i=1,10 do
|
||||
local address = skynet.newservice("testgroup_c", tostring(i))
|
||||
table.insert(g, address)
|
||||
group.enter(gid , address)
|
||||
end
|
||||
skynet.cast(g,"text","Cast")
|
||||
skynet.sleep(1000)
|
||||
skynet.send(gaddr,"text","Hello World")
|
||||
skynet.exit()
|
||||
end)
|
||||
13
test/testgroup_c.lua
Normal file
13
test/testgroup_c.lua
Normal file
@@ -0,0 +1,13 @@
|
||||
local skynet = require "skynet"
|
||||
local group = require "mcgroup"
|
||||
--local group = require "localgroup"
|
||||
local id, g = ...
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("text",function(session,address,text)
|
||||
print("===>",id, text)
|
||||
end)
|
||||
if g then
|
||||
group.enter(tonumber(g))
|
||||
end
|
||||
end)
|
||||
9
test/testlog.lua
Normal file
9
test/testlog.lua
Normal file
@@ -0,0 +1,9 @@
|
||||
local skynet = require "skynet"
|
||||
local log = require "log"
|
||||
|
||||
skynet.start(function()
|
||||
log.Info("hello world")
|
||||
skynet.exit()
|
||||
end)
|
||||
|
||||
|
||||
10
test/testping.lua
Normal file
10
test/testping.lua
Normal file
@@ -0,0 +1,10 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
skynet.start(function()
|
||||
local ps = skynet.uniqueservice("pingserver")
|
||||
skynet.watch(ps)
|
||||
-- print(pcall(skynet.call,ps,"lua","ERROR"))
|
||||
print(skynet.call(ps, "lua", "PING", "hello"))
|
||||
print(skynet.call(ps, "lua", "PING", "hay"))
|
||||
skynet.send(ps, "lua", "EXIT")
|
||||
end)
|
||||
10
test/testqueue.lua
Normal file
10
test/testqueue.lua
Normal file
@@ -0,0 +1,10 @@
|
||||
local skynet = require "skynet"
|
||||
local mqueue = require "mqueue"
|
||||
|
||||
skynet.start(function()
|
||||
local pingqueue = skynet.newservice "pingqueue"
|
||||
print(mqueue.call(pingqueue, "A"))
|
||||
print(mqueue.call(pingqueue, "B"))
|
||||
print(mqueue.call(pingqueue, "C"))
|
||||
skynet.exit()
|
||||
end)
|
||||
61
test/testredis.lua
Normal file
61
test/testredis.lua
Normal file
@@ -0,0 +1,61 @@
|
||||
local skynet = require "skynet"
|
||||
local redis = require "redis"
|
||||
|
||||
local function watching()
|
||||
local w = redis.watch "main"
|
||||
w:subscribe "foo"
|
||||
w:psubscribe "hello.*"
|
||||
while true do
|
||||
print("Watch", w:message())
|
||||
end
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.fork(watching)
|
||||
local db = redis.connect "main"
|
||||
|
||||
db:del "C"
|
||||
db:set("A", "hello")
|
||||
db:set("B", "world")
|
||||
db:sadd("C", "one")
|
||||
|
||||
print(db:get("A"))
|
||||
print(db:get("B"))
|
||||
|
||||
db:del "D"
|
||||
for i=1,10 do
|
||||
db:hset("D",i,i)
|
||||
end
|
||||
local r = db:hvals "D"
|
||||
for k,v in pairs(r) do
|
||||
print(k,v)
|
||||
end
|
||||
|
||||
db:multi()
|
||||
db:get "A"
|
||||
db:get "B"
|
||||
local t = db:exec()
|
||||
for k,v in ipairs(t) do
|
||||
print("Exec", v)
|
||||
end
|
||||
|
||||
print(db:exists "A")
|
||||
print(db:get "A")
|
||||
print(db:set("A","hello world"))
|
||||
print(db:get("A"))
|
||||
print(db:sismember("C","one"))
|
||||
print(db:sismember("C","two"))
|
||||
|
||||
print("===========publish============")
|
||||
|
||||
for i=1,10 do
|
||||
db:publish("foo", i)
|
||||
end
|
||||
for i=11,20 do
|
||||
db:publish("hello.foo", i)
|
||||
end
|
||||
|
||||
db:disconnect()
|
||||
-- skynet.exit()
|
||||
end)
|
||||
|
||||
51
test/testsocket.lua
Normal file
51
test/testsocket.lua
Normal file
@@ -0,0 +1,51 @@
|
||||
local skynet = require "skynet"
|
||||
local socket = require "socket"
|
||||
|
||||
local mode , id = ...
|
||||
|
||||
local function echo(id)
|
||||
socket.start(id)
|
||||
|
||||
while true do
|
||||
local str = socket.readline(id,"\n")
|
||||
if str then
|
||||
socket.write(id, str .. "\n")
|
||||
else
|
||||
socket.close(id)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if mode == "agent" then
|
||||
id = tonumber(id)
|
||||
|
||||
skynet.start(function()
|
||||
skynet.fork(function()
|
||||
echo(id)
|
||||
skynet.exit()
|
||||
end)
|
||||
end)
|
||||
else
|
||||
local function accept(id)
|
||||
socket.start(id)
|
||||
socket.write(id, "Hello Skynet\n")
|
||||
skynet.newservice("testsocket", "agent", id)
|
||||
-- notice: Some data on this connection(id) may lost before new service start.
|
||||
-- So, be careful when you want to use start / abandon / start .
|
||||
socket.abandon(id)
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
local id = socket.listen("127.0.0.1", 8000)
|
||||
|
||||
socket.start(id , function(id, addr)
|
||||
print("connect from " .. addr .. " " .. id)
|
||||
-- you have choices :
|
||||
-- 1. skynet.newservice("testsocket", "agent", id)
|
||||
-- 2. skynet.fork(echo, id)
|
||||
-- 3. accept(id)
|
||||
accept(id)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
34
test/testtimer.lua
Normal file
34
test/testtimer.lua
Normal file
@@ -0,0 +1,34 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
local function timeout(t)
|
||||
print(t)
|
||||
end
|
||||
|
||||
local function wakeup(co)
|
||||
for i=1,5 do
|
||||
skynet.sleep(50)
|
||||
skynet.wakeup(co)
|
||||
end
|
||||
end
|
||||
|
||||
local function test()
|
||||
skynet.timeout(10, function() print("test timeout 10") end)
|
||||
for i=1,10 do
|
||||
print("test sleep",i,skynet.now())
|
||||
skynet.sleep(1)
|
||||
end
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
test()
|
||||
|
||||
skynet.fork(wakeup, coroutine.running())
|
||||
skynet.timeout(300, function() timeout "Hello World" end)
|
||||
for i = 1, 10 do
|
||||
print(i, skynet.now())
|
||||
print(skynet.sleep(100))
|
||||
end
|
||||
skynet.exit()
|
||||
print("Test timer exit")
|
||||
|
||||
end)
|
||||
28
test/testtrace.lua
Normal file
28
test/testtrace.lua
Normal file
@@ -0,0 +1,28 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
local trace_cache = {}
|
||||
|
||||
skynet.trace_callback(function(handle, ti)
|
||||
-- skynet will call this function when the thread which create handle(traceid) end, and pass the time (ti)
|
||||
print("TRACE",trace_cache[handle],ti)
|
||||
trace_cache[handle] = nil
|
||||
end)
|
||||
|
||||
local function f (i)
|
||||
-- create a trace object
|
||||
local traceid = skynet.trace()
|
||||
-- name traceid in trace_cache[]
|
||||
trace_cache[traceid] = "thread " .. i
|
||||
|
||||
for i=1,i do
|
||||
skynet.sleep(i)
|
||||
end
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
for i = 1 , 100 do
|
||||
skynet.fork(f, i)
|
||||
end
|
||||
end)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user