Skip to content

Commit 438ec91

Browse files
authored
Merge pull request #318 from fmv1992/nojira_270
NOJIRA-270: add `retry` macro; add it to a failing test
2 parents 839d1a2 + 70126b0 commit 438ec91

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Sagittarius uses CMake for its building infrastructure. If you do not
1414
have it on your platform, please install it.
1515

1616
- [CMake](http://www.cmake.org/)
17-
17+
1818
NOTE: It should be higher than 3.5, though we don't use new features, so
1919
it should also work with 2.8.4
2020

@@ -25,6 +25,7 @@ install to default location, run the following commands in the
2525
directory where all distributed files are expanded (c.f. By default
2626
it'd be `sagittarius-X.X.X`, `X.X.X` is the version you downloaded):
2727

28+
% ./dist.sh gen
2829
% cmake .
2930
% make
3031
% make install
@@ -87,7 +88,7 @@ for example:
8788
To run the tests, specify `test` target.
8889

8990
% make test
90-
91+
9192
Or, alternatively, you can also use `ctest`. This is convenient to
9293
test individual tests.
9394

@@ -138,7 +139,7 @@ Only with Homebrew is tested. A user can install sagittarius directly with
138139
homebrew, via
139140

140141
$ brew install sagittarius-scheme
141-
142+
142143
Alternately, the user can install the following dependencies and then make
143144
sagittarius locally.
144145

@@ -226,7 +227,7 @@ of the test execution when the test failed.
226227
% ctest --output-on-failure
227228
```
228229

229-
For more options, please refer the official document of the
230+
For more options, please refer the official document of the
230231
[`CTest`](https://cmake.org/cmake/help/latest/manual/ctest.1.html)
231232

232233
# Forums and bug reporting

sitelib/sagittarius/test/helper.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@
5656
((show)
5757
(lambda ()
5858
(show))))))
59-
)
59+
)

test/tests/net/server.scm

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
;; multi threading server
3939
(let ()
4040
(define config (make-server-config :shutdown-port +shutdown-port+
41-
:exception-handler
41+
:exception-handler
4242
(lambda (sr s e) (print e))
4343
:max-thread 5
4444
:use-ipv6? #t))
@@ -102,7 +102,7 @@
102102
(let ((sock (make-client-tls-socket "localhost" (server-port server)
103103
ai-family)))
104104
(socket-send sock (string->utf8 "hello"))
105-
(test-equal "TLS echo back"
105+
(test-equal "TLS echo back"
106106
(string->utf8 "hello") (socket-recv sock 255))
107107
(socket-close sock))))
108108
(server-start! server :background #t)
@@ -130,21 +130,30 @@
130130
(test-equal 'context (server-context server))
131131
(test-error (server-status server)))
132132

133+
;; Test for socket detachment functionality in the simple server framework.
134+
;;
135+
;; This test verifies that a server can detach sockets and hand them off to
136+
;; external actors for processing, while maintaining proper thread pool status.
137+
;; The test creates a non-blocking server that detaches incoming connections
138+
;; to a shared-queue-channel-actor which handles the actual socket
139+
;; communication.
133140
(let ()
134141
;; the thread management is done outside of our threads
135142
;; thus there's no way to guarantee. let's hope...
136143
(define (hope-it-works)
137144
(thread-yield!)
138145
(thread-sleep! 1))
146+
;; Actor that receives detached sockets and handles them independently.
139147
(define detached-actor
140148
(make-shared-queue-channel-actor
141149
(lambda (input-receiver output-sender)
142150
(define socket (input-receiver))
143151
(output-sender 'ready)
144152
(hope-it-works)
145153
(let ((msg (input-receiver)))
146-
(socket-send socket msg))
154+
(socket-send socket msg))
147155
(output-sender 'done)
156+
;; Wait for finish signal.
148157
(input-receiver)
149158
(socket-shutdown socket SHUT_RDWR)
150159
(socket-close socket))))
@@ -153,7 +162,9 @@
153162
:exception-handler print))
154163
(define server (make-simple-server
155164
"12345" (lambda (s sock)
165+
;; Remove socket from server's management.
156166
(server-detach-socket! s sock)
167+
;; Hand it over to external actor.
157168
(actor-send-message! detached-actor sock))
158169
:config config))
159170
(define (check-status server)
@@ -170,26 +181,32 @@
170181
(test-assert
171182
(call-with-string-output-port
172183
(lambda (out) (report-server-status status out))))))
173-
184+
174185
(server-start! server :background #t)
175186
(test-assert (server-status server))
176187
(check-status server)
177188

178189
(let ((sock (make-client-socket "localhost" "12345")))
190+
;; Trigger socket detachment by connecting.
179191
(socket-send sock #vu8(0))
180192
(test-equal 'ready (actor-receive-message! detached-actor))
193+
;; Send actual data through the detached socket.
181194
(actor-send-message! detached-actor #vu8(1 2 3 4 5))
182195
(test-equal 'done (actor-receive-message! detached-actor))
183196
(hope-it-works)
184197
;; it should have 0 active socket on the server, it's detached
185198
;; and server socket is not closed
186199
(check-status server)
200+
;; Signal actor to finish and close socket.
187201
(actor-send-message! detached-actor 'finish)
188-
(let ((bv (socket-recv sock 5)))
189-
(test-equal #vu8(1 2 3 4 5) bv))
202+
;; Handle potential race condition where socket closes before read.
203+
(guard (e ((socket-error? e) (test-assert "server socket closed" #t))
204+
(else (test-assert (condition-message e) #f)))
205+
(let ((bv (socket-recv sock 5)))
206+
(test-equal #vu8(1 2 3 4 5) bv)))
190207
(socket-shutdown sock SHUT_RDWR)
191208
(socket-close sock))
192-
209+
193210
(server-stop! server))
194211

195212
(test-end)

0 commit comments

Comments
 (0)