-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpy_fd_ops_SUITE.erl
More file actions
144 lines (117 loc) · 3.68 KB
/
py_fd_ops_SUITE.erl
File metadata and controls
144 lines (117 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
%% Copyright 2026 Benoit Chesneau
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%% @doc Test suite for direct FD operations (fd_read, fd_write, fd_close, socketpair).
-module(py_fd_ops_SUITE).
-include_lib("common_test/include/ct.hrl").
-export([
all/0,
init_per_suite/1,
end_per_suite/1
]).
-export([
socketpair_test/1,
fd_read_write_test/1,
fd_close_test/1,
fd_select_test/1
]).
all() ->
[
socketpair_test,
fd_read_write_test,
fd_close_test,
fd_select_test
].
init_per_suite(Config) ->
application:ensure_all_started(erlang_python),
Config.
end_per_suite(_Config) ->
ok.
%%% ============================================================================
%%% Test Cases
%%% ============================================================================
%% @doc Test socketpair creation
socketpair_test(_Config) ->
{ok, {Fd1, Fd2}} = py_nif:socketpair(),
true = is_integer(Fd1),
true = is_integer(Fd2),
true = Fd1 >= 0,
true = Fd2 >= 0,
true = Fd1 =/= Fd2,
%% Clean up
ok = py_nif:fd_close(Fd1),
ok = py_nif:fd_close(Fd2),
ok.
%% @doc Test fd_read and fd_write
fd_read_write_test(_Config) ->
{ok, {Fd1, Fd2}} = py_nif:socketpair(),
%% Write to Fd1, read from Fd2
TestData = <<"hello world">>,
{ok, Written} = py_nif:fd_write(Fd1, TestData),
Written = byte_size(TestData),
%% Small delay to ensure data is available
timer:sleep(10),
{ok, ReadData} = py_nif:fd_read(Fd2, 1024),
TestData = ReadData,
%% Write to Fd2, read from Fd1
TestData2 = <<"response data">>,
{ok, Written2} = py_nif:fd_write(Fd2, TestData2),
Written2 = byte_size(TestData2),
timer:sleep(10),
{ok, ReadData2} = py_nif:fd_read(Fd1, 1024),
TestData2 = ReadData2,
%% Clean up
ok = py_nif:fd_close(Fd1),
ok = py_nif:fd_close(Fd2),
ok.
%% @doc Test fd_close
fd_close_test(_Config) ->
{ok, {Fd1, Fd2}} = py_nif:socketpair(),
%% Close should succeed
ok = py_nif:fd_close(Fd1),
ok = py_nif:fd_close(Fd2),
%% Reading from closed fd should fail
{error, _} = py_nif:fd_read(Fd1, 1024),
ok.
%% @doc Test fd_select_read and fd_select_write
fd_select_test(_Config) ->
{ok, {Fd1, Fd2}} = py_nif:socketpair(),
%% Register for read on Fd2 - returns {ok, FdRef}
{ok, ReadRef} = py_nif:fd_select_read(Fd2),
true = is_reference(ReadRef),
%% Write data to Fd1 (should trigger read ready on Fd2)
TestData = <<"select test">>,
{ok, _} = py_nif:fd_write(Fd1, TestData),
%% Should receive select message with the FdRef
receive
{select, ReadRef, _Ref, ready_input} ->
ok
after 1000 ->
ct:fail(select_timeout)
end,
%% Read the data
{ok, ReadData} = py_nif:fd_read(Fd2, 1024),
TestData = ReadData,
%% Test write select - sockets are usually immediately writable
{ok, WriteRef} = py_nif:fd_select_write(Fd1),
true = is_reference(WriteRef),
receive
{select, WriteRef, _Ref2, ready_output} ->
ok
after 1000 ->
ct:fail(write_select_timeout)
end,
%% Clean up
ok = py_nif:fd_close(Fd1),
ok = py_nif:fd_close(Fd2),
ok.