summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorruki <[email protected]>2019-11-04 22:20:02 +0800
committerGitHub <[email protected]>2019-11-04 22:20:02 +0800
commitb1eee1ec65dd0f9d3a26af52596d86c331160587 (patch)
tree6811a354f07c146ba4417e649d3a31d0c9aabd45 /tests
parent20b40c57bbe9d28f822d5feac304a5b892b02790 (diff)
parentf00316a42943a9a75303658f8d646d35f9e8d1b9 (diff)
Merge pull request #593 from xmake-io/socket
Add io.socket support
Diffstat (limited to 'tests')
-rw-r--r--tests/modules/bytes/test.lua37
-rw-r--r--tests/modules/socket/tcp/echo_client.lua21
-rw-r--r--tests/modules/socket/tcp/echo_server.lua32
-rw-r--r--tests/modules/socket/tcp/file_client.lua35
-rw-r--r--tests/modules/socket/tcp/file_server.lua24
-rw-r--r--tests/modules/socket/udp/echo_client.lua15
-rw-r--r--tests/modules/socket/udp/echo_server.lua18
7 files changed, 182 insertions, 0 deletions
diff --git a/tests/modules/bytes/test.lua b/tests/modules/bytes/test.lua
new file mode 100644
index 000000000..52b754c0a
--- /dev/null
+++ b/tests/modules/bytes/test.lua
@@ -0,0 +1,37 @@
+
+function test_ctor(t)
+ t:are_equal(bytes("123456789"):str(), "123456789")
+ t:are_equal(bytes(bytes("123456789")):str(), "123456789")
+ t:are_equal(bytes(bytes("123456789"), 3, 5):str(), "345")
+ t:are_equal(bytes("123456789"):size(), 9)
+ t:are_equal(bytes(10):size(), 10)
+ t:are_equal(bytes(bytes("123"), bytes("456"), bytes("789")):str(), "123456789")
+ t:are_equal(bytes({bytes("123"), bytes("456"), bytes("789")}):str(), "123456789")
+end
+
+function test_clone(t)
+ t:are_equal(bytes(10):clone():size(), 10)
+ t:are_equal(bytes("123456789"):clone():str(), "123456789")
+end
+
+function test_slice(t)
+ t:are_equal(bytes(10):slice(1, 2):size(), 2)
+ t:are_equal(bytes("123456789"):slice(1, 4):str(), "1234")
+end
+
+function test_index(t)
+ local b = bytes("123456789")
+ t:are_equal(b[{1, 4}]:str(), "1234")
+ t:will_raise(function() b[1] = string.byte('2') end)
+ b = bytes(9)
+ b[{1, 9}] = bytes("123456789")
+ t:are_equal(b:str(), "123456789")
+ b[1] = string.byte('2')
+ t:are_equal(b:str(), "223456789")
+ t:will_raise(function() b[100] = string.byte('2') end)
+end
+
+function test_concat(t)
+ t:are_equal((bytes("123") .. bytes("456")):str(), "123456")
+ t:are_equal(bytes(bytes("123"), bytes("456")):str(), "123456")
+end
diff --git a/tests/modules/socket/tcp/echo_client.lua b/tests/modules/socket/tcp/echo_client.lua
new file mode 100644
index 000000000..f3d6d9bae
--- /dev/null
+++ b/tests/modules/socket/tcp/echo_client.lua
@@ -0,0 +1,21 @@
+import("core.base.socket")
+
+function main()
+ local addr = "127.0.0.1"
+ local port = 9001
+ print("connect %s:%d ..", addr, port)
+ local sock = socket.connect(addr, port)
+ print("%s: connected!", sock)
+ local count = 0
+ while count < 10000 do
+ local send = sock:send("hello world..", {block = true})
+ if send > 0 then
+ sock:recv(13, {block = true})
+ else
+ break
+ end
+ count = count + 1
+ end
+ print("%s: send ok, count: %d!", sock, count)
+ sock:close()
+end
diff --git a/tests/modules/socket/tcp/echo_server.lua b/tests/modules/socket/tcp/echo_server.lua
new file mode 100644
index 000000000..9ff19dbed
--- /dev/null
+++ b/tests/modules/socket/tcp/echo_server.lua
@@ -0,0 +1,32 @@
+import("core.base.socket")
+
+function main()
+
+ local addr = "127.0.0.1"
+ local port = 9001
+ local sock = socket.bind(addr, port)
+ sock:listen(20)
+ print("%s: listening %s:%d ..", sock, addr, port)
+ while true do
+ local sock_client = sock:accept()
+ if sock_client then
+ print("%s: accepted", sock_client)
+ local count = 0
+ local result = nil
+ while true do
+ local recv, data = sock_client:recv(13, {block = true})
+ if recv > 0 then
+ result = data
+ sock_client:send(data, {block = true})
+ count = count + 1
+ else
+ break
+ end
+ end
+ print("%s: recv: %d, count: %d", sock_client, result and result:size() or 0, count)
+ result:dump()
+ sock_client:close()
+ end
+ end
+ sock:close()
+end
diff --git a/tests/modules/socket/tcp/file_client.lua b/tests/modules/socket/tcp/file_client.lua
new file mode 100644
index 000000000..42532f5fe
--- /dev/null
+++ b/tests/modules/socket/tcp/file_client.lua
@@ -0,0 +1,35 @@
+import("core.base.socket")
+
+function main()
+ local addr = "127.0.0.1"
+ local port = 9090
+ print("connect %s:%d ..", addr, port)
+ local sock = socket.connect(addr, port)
+ print("%s: connected!", sock)
+ local real = 0
+ local recv = 0
+ local data = nil
+ local wait = false
+ local results = {}
+ while true do
+ real, data = sock:recv(8192)
+ if real > 0 then
+ recv = recv + real
+ wait = false
+ table.insert(results, data)
+ elseif real == 0 and not wait then
+ if sock:wait(socket.EV_RECV, -1) == socket.EV_RECV then
+ wait = true
+ else
+ break
+ end
+ else
+ break
+ end
+ end
+ if #results > 0 then
+ data = bytes(results)
+ end
+ print("%s: recv ok, size: %d, #data: %d!", sock, recv, data and data:size() or 0)
+ sock:close()
+end
diff --git a/tests/modules/socket/tcp/file_server.lua b/tests/modules/socket/tcp/file_server.lua
new file mode 100644
index 000000000..d619a496f
--- /dev/null
+++ b/tests/modules/socket/tcp/file_server.lua
@@ -0,0 +1,24 @@
+import("core.base.socket")
+
+function main(filepath)
+
+ local addr = "127.0.0.1"
+ local port = 9090
+ local sock = socket.bind(addr, port)
+ sock:listen(20)
+ print("%s: listening %s:%d ..", sock, addr, port)
+ while true do
+ local sock_client = sock:accept()
+ if sock_client then
+ print("%s: accepted", sock_client)
+ local file = io.open(filepath, 'rb')
+ if file then
+ local send = sock_client:sendfile(file, {block = true})
+ print("%s: send %s %d bytes!", sock_client, filepath, send)
+ file:close()
+ end
+ sock_client:close()
+ end
+ end
+ sock:close()
+end
diff --git a/tests/modules/socket/udp/echo_client.lua b/tests/modules/socket/udp/echo_client.lua
new file mode 100644
index 000000000..f46ae4716
--- /dev/null
+++ b/tests/modules/socket/udp/echo_client.lua
@@ -0,0 +1,15 @@
+import("core.base.socket")
+
+function main(data)
+ local addr = "127.0.0.1"
+ local port = 9001
+ local sock = socket.udp()
+ local send = sock:sendto(data or "hello xmake!", addr, port, {block = true})
+ print("%s: send to %s:%d %d bytes!", sock, addr, port, send)
+ local recv, data, peer_addr, peer_port = sock:recvfrom(8112, {block = true})
+ if recv > 0 then
+ print("%s: recv %d bytes from %s:%d", sock, recv, peer_addr, peer_port)
+ data:dump()
+ end
+ sock:close()
+end
diff --git a/tests/modules/socket/udp/echo_server.lua b/tests/modules/socket/udp/echo_server.lua
new file mode 100644
index 000000000..84f55eae4
--- /dev/null
+++ b/tests/modules/socket/udp/echo_server.lua
@@ -0,0 +1,18 @@
+import("core.base.socket")
+
+function main()
+ local addr = "127.0.0.1"
+ local port = 9001
+ local sock = socket.udp()
+ sock:bind(addr, port)
+ while true do
+ print("%s: recv in %s:%d ..", sock, addr, port)
+ local recv, data, peer_addr, peer_port = sock:recvfrom(8192, {block = true})
+ print("%s: recv %d bytes from: %s:%d", sock, recv, peer_addr, peer_port)
+ if data then
+ data:dump()
+ sock:sendto(data, peer_addr, peer_port)
+ end
+ end
+ sock:close()
+end