Skip to content

Commit 7a41fef

Browse files
authored
more tcp tests (#370)
* more tcp tests * fix tests
1 parent a82e565 commit 7a41fef

File tree

5 files changed

+138
-6
lines changed

5 files changed

+138
-6
lines changed

ci/all_tests.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ rm -rf dirExampleE
9797
rm -rf dirExampleA
9898
rm -rf dirExampleD
9999

100+
# countdown, echo, form... all require user input or special setup
101+
ignore_list=("stdin-basic.roc" "stdin-pipe.roc" "command-line-args.roc" "http.roc" "env-var.roc" "bytes-stdin-stdout.roc" "error-handling.roc" "tcp-client.roc" "tcp.roc" "terminal-app-snake.roc")
102+
100103
# roc dev (some expects only run with `roc dev`)
101104
for roc_file in $EXAMPLES_DIR*.roc; do
102105
base_file=$(basename "$roc_file")
103106

104-
# countdown, echo, form... all require user input or special setup
105-
ignore_list=("stdin-basic.roc" "stdin-pipe.roc" "command-line-args.roc" "http.roc" "env-var.roc" "bytes-stdin-stdout.roc" "error-handling.roc" "tcp-client.roc" "terminal-app-snake.roc")
106107

107108
# check if base_file matches something from ignore_list
108109
for file in "${ignore_list[@]}"; do
@@ -130,9 +131,17 @@ for roc_file in $EXAMPLES_DIR*.roc; do
130131
$ROC dev $roc_file $ROC_BUILD_FLAGS
131132
fi
132133
done
134+
133135
for roc_file in $TESTS_DIR*.roc; do
134136
base_file=$(basename "$roc_file")
135137

138+
# check if base_file matches something from ignore_list
139+
for file in "${ignore_list[@]}"; do
140+
if [ "$base_file" == "$file" ]; then
141+
continue 2 # continue the outer loop if a match is found
142+
fi
143+
done
144+
136145
if [ "$base_file" == "sqlite.roc" ]; then
137146
DB_PATH=${TESTS_DIR}test.db $ROC dev $roc_file $ROC_BUILD_FLAGS
138147
else

ci/expect_scripts/tcp.exp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/expect
2+
3+
# uncomment line below for debugging
4+
# exp_internal 1
5+
6+
set timeout 7
7+
8+
source ./ci/expect_scripts/shared-code.exp
9+
10+
# get path to cat command
11+
set cat_path [exec which cat]
12+
# Start echo server
13+
spawn ncat -e $cat_path -l 8085
14+
sleep 5
15+
16+
spawn $env(TESTS_DIR)tcp
17+
18+
19+
expect "Testing Tcp module functions" {
20+
expect "Successfully connected to localhost" {
21+
expect "Echo server reply: Hello" {
22+
expect "Echo server reply: Test message from Roc!" {
23+
expect "Tcp.read_up_to yielded: 'do not read past me'" {
24+
expect "Tcp.read_exactly yielded: 'ABC'" {
25+
expect "Tcp.read_until yielded: 'Line1\r\n'" {
26+
expect "✓ All tests executed! ✓\r\n" {
27+
expect eof {
28+
check_exit_and_segfault
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
38+
39+
puts stderr "\nExpect script failed: output was different from expected value."
40+
exit 1

flake.nix

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
rust =
3131
pkgs.rust-bin.fromRustupToolchainFile "${toString ./rust-toolchain.toml}";
3232

33+
aliases = ''
34+
alias testcmd='export ROC=roc && export EXAMPLES_DIR=./examples/ && ./ci/all_tests.sh'
35+
'';
36+
3337
linuxInputs = with pkgs;
3438
lib.optionals stdenv.isLinux [
3539
valgrind
@@ -64,6 +68,10 @@
6468
# for crti.o, crtn.o, and Scrt1.o
6569
NIX_GLIBC_PATH =
6670
if pkgs.stdenv.isLinux then "${pkgs.glibc.out}/lib" else "";
71+
72+
shellHook = ''
73+
${aliases}
74+
'';
6775
};
6876

6977
formatter = pkgs.nixpkgs-fmt;

tests/.gitignore

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
sqlite
2-
utc
3-
url
4-
env
1+
# Ignore all files including binary files that have no extension
2+
*
3+
# Unignore all files with extensions
4+
!*.*
5+
# Unignore all directories
6+
!*/

tests/tcp.roc

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
app [main!] { pf: platform "../platform/main.roc" }
2+
3+
import pf.Stdout
4+
import pf.Tcp
5+
import pf.Arg exposing [Arg]
6+
7+
main! : List Arg => Result {} _
8+
main! = |_args|
9+
Stdout.line!("Testing Tcp module functions...")?
10+
Stdout.line!("Note: These tests require a TCP server running on localhost:8085")?
11+
Stdout.line!("You can start one with: ncat -e `which cat` -l 8085")?
12+
Stdout.line!("")?
13+
14+
Stdout.line!("Testing Tcp.connect!:")?
15+
when Tcp.connect!("127.0.0.1", 8085) is
16+
Ok(stream) ->
17+
Stdout.line!("✓ Successfully connected to localhost:8085")?
18+
test_tcp_functions!(stream)?
19+
Stdout.line!("\n✓ All tests executed! ✓")
20+
21+
Err(connect_err) ->
22+
err_str = Tcp.connect_err_to_str(connect_err)
23+
Err(Exit(1, "Failed to connect: ${err_str}"))
24+
25+
26+
test_tcp_functions! : Tcp.Stream => Result {} _
27+
test_tcp_functions! = |stream|
28+
29+
Stdout.line!("\nTesting Tcp.write!:")?
30+
hello_bytes = [72, 101, 108, 108, 111, 10] # "Hello\n" in bytes
31+
Tcp.write!(stream, hello_bytes)?
32+
33+
reply_msg = Tcp.read_line!(stream)?
34+
Stdout.line!("Echo server reply: ${reply_msg}")?
35+
36+
37+
Stdout.line!("Testing Tcp.write_utf8!:")?
38+
test_message = "Test message from Roc!\n"
39+
Tcp.write_utf8!(stream, test_message)?
40+
41+
reply_msg_utf8 = Tcp.read_line!(stream)?
42+
Stdout.line!("Echo server reply: ${reply_msg_utf8}")?
43+
44+
45+
Stdout.line!("Testing Tcp.read_up_to!:")?
46+
47+
do_not_read_bytes = [100, 111, 32, 110, 111, 116, 32, 114, 101, 97, 100, 32, 112, 97, 115, 116, 32, 109, 101, 65] # "do not read past meA" in bytes
48+
Tcp.write!(stream, do_not_read_bytes)?
49+
50+
nineteen_bytes = Tcp.read_up_to!(stream, 19) ? |err| FailedReadUpTo(err)
51+
nineteen_bytes_as_str = Str.from_utf8(nineteen_bytes) ? |err| ReadUpToFromUtf8(err)
52+
53+
Stdout.line!("Tcp.read_up_to yielded: '${nineteen_bytes_as_str}'")?
54+
55+
56+
Stdout.line!("\nTesting Tcp.read_exactly!:")?
57+
Tcp.write_utf8!(stream, "BC")?
58+
59+
three_bytes = Tcp.read_exactly!(stream, 3) ? |err| FailedReadExactly(err)
60+
three_bytes_as_str = Str.from_utf8(three_bytes) ? |err| ReadExactlyFromUtf8(err)
61+
62+
Stdout.line!("Tcp.read_exactly yielded: '${three_bytes_as_str}'")?
63+
64+
65+
Stdout.line!("\nTesting Tcp.read_until!:")?
66+
Tcp.write_utf8!(stream, "Line1\nLine2\n")?
67+
68+
bytes_until = Tcp.read_until!(stream, '\n') ? |err| FailedReadUntil(err)
69+
bytes_until_as_str = Str.from_utf8(bytes_until) ? |err| ReadUntilFromUtf8(err)
70+
71+
Stdout.line!("Tcp.read_until yielded: '${bytes_until_as_str}'")?
72+
73+
Ok({})

0 commit comments

Comments
 (0)