aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorChristopher Baines <mail@cbaines.net>2024-11-19 18:43:43 +0000
committerChristopher Baines <mail@cbaines.net>2024-12-16 09:18:12 +0000
commit2f39c58d6ca72cd869ba69e03d639f36d497e9a8 (patch)
tree78caa9af60eb057eda659e969207a8457e2fa3f2 /tests
downloadknots-2f39c58d6ca72cd869ba69e03d639f36d497e9a8.tar
knots-2f39c58d6ca72cd869ba69e03d639f36d497e9a8.tar.gz
Initial commit
Diffstat (limited to 'tests')
-rw-r--r--tests/non-blocking.scm31
-rw-r--r--tests/parallelism.scm15
-rw-r--r--tests/promise.scm20
-rw-r--r--tests/queue.scm22
-rw-r--r--tests/resource-pool.scm18
-rw-r--r--tests/timeout.scm22
-rw-r--r--tests/web-server.scm30
-rw-r--r--tests/worker-threads.scm32
8 files changed, 190 insertions, 0 deletions
diff --git a/tests/non-blocking.scm b/tests/non-blocking.scm
new file mode 100644
index 0000000..0f81f58
--- /dev/null
+++ b/tests/non-blocking.scm
@@ -0,0 +1,31 @@
+(use-modules (tests)
+ (fibers)
+ (unit-test)
+ (web uri)
+ (web client)
+ (web response)
+ (knots web-server)
+ (knots non-blocking))
+
+(run-fibers-for-tests
+ (lambda ()
+ (let* ((web-server
+ (run-knots-web-server
+ (lambda (request)
+ (values '((content-type . (text/plain)))
+ "Hello, World!"))
+ #:port 0)) ;; Bind to any port
+ (port
+ (web-server-port web-server))
+ (uri
+ (build-uri 'http #:host "127.0.0.1" #:port port)))
+
+
+ (assert-equal
+ 200
+ (response-code
+ (http-get
+ uri
+ #:port (nonblocking-open-socket-for-uri uri)))))))
+
+(display "non-blocking test finished successfully\n")
diff --git a/tests/parallelism.scm b/tests/parallelism.scm
new file mode 100644
index 0000000..8249d68
--- /dev/null
+++ b/tests/parallelism.scm
@@ -0,0 +1,15 @@
+(use-modules (tests)
+ (fibers)
+ (unit-test)
+ (knots parallelism))
+
+(run-fibers-for-tests
+ (lambda ()
+ (assert-equal
+ 1122
+ (apply + (fibers-map
+ (lambda (i)
+ (* 2 i))
+ (iota 34))))))
+
+(display "parallelism test finished successfully\n")
diff --git a/tests/promise.scm b/tests/promise.scm
new file mode 100644
index 0000000..b7dec73
--- /dev/null
+++ b/tests/promise.scm
@@ -0,0 +1,20 @@
+(use-modules (tests)
+ (fibers)
+ (unit-test)
+ (knots parallelism)
+ (knots promise))
+
+(run-fibers-for-tests
+ (lambda ()
+ (let ((promises
+ (map (lambda (i)
+ (fibers-delay
+ (lambda ()
+ (* i 2))))
+ (iota 10))))
+
+ (assert-equal
+ 90
+ (apply + (fibers-map fibers-force promises))))))
+
+(display "promise test finished successfully\n")
diff --git a/tests/queue.scm b/tests/queue.scm
new file mode 100644
index 0000000..c80e5fd
--- /dev/null
+++ b/tests/queue.scm
@@ -0,0 +1,22 @@
+(use-modules (tests)
+ (fibers)
+ (fibers channels)
+ (unit-test)
+ (knots queue))
+
+(run-fibers-for-tests
+ (lambda ()
+ (let* ((dest-channel
+ (make-channel))
+ (queue-channel
+ (spawn-queueing-fiber dest-channel)))
+
+ (put-message queue-channel 1)
+ (put-message queue-channel 2)
+ (put-message queue-channel 3)
+
+ (assert-equal 1 (get-message dest-channel))
+ (assert-equal 2 (get-message dest-channel))
+ (assert-equal 3 (get-message dest-channel)))))
+
+(display "queue test finished successfully\n")
diff --git a/tests/resource-pool.scm b/tests/resource-pool.scm
new file mode 100644
index 0000000..ebd4682
--- /dev/null
+++ b/tests/resource-pool.scm
@@ -0,0 +1,18 @@
+(use-modules (tests)
+ (fibers)
+ (unit-test)
+ (knots resource-pool))
+
+(run-fibers-for-tests
+ (lambda ()
+ (let ((resource-pool (make-resource-pool
+ (lambda ()
+ 2)
+ 1)))
+ (assert-equal
+ (with-resource-from-pool resource-pool
+ res
+ res)
+ 2))))
+
+(display "resource-pool test finished successfully\n")
diff --git a/tests/timeout.scm b/tests/timeout.scm
new file mode 100644
index 0000000..bab39d2
--- /dev/null
+++ b/tests/timeout.scm
@@ -0,0 +1,22 @@
+(use-modules (tests)
+ (fibers)
+ (unit-test)
+ (knots timeout))
+
+(run-fibers-for-tests
+ (lambda ()
+ (assert-equal
+ 1
+ (with-fibers-timeout
+ (const 1)
+ #:timeout 10))
+
+ (assert-equal
+ 2
+ (with-fibers-timeout
+ (lambda ()
+ (sleep 10))
+ #:timeout 0.1
+ #:on-timeout (const 2)))))
+
+(display "timeout test finished successfully\n")
diff --git a/tests/web-server.scm b/tests/web-server.scm
new file mode 100644
index 0000000..5a76d0f
--- /dev/null
+++ b/tests/web-server.scm
@@ -0,0 +1,30 @@
+(use-modules (tests)
+ (fibers)
+ (unit-test)
+ (web uri)
+ (web client)
+ (web response)
+ (knots web-server)
+ (knots non-blocking))
+
+(run-fibers-for-tests
+ (lambda ()
+ (let* ((web-server
+ (run-knots-web-server
+ (lambda (request)
+ (values '((content-type . (text/plain)))
+ "Hello, World!"))
+ #:port 0)) ;; Bind to any port
+ (port
+ (web-server-port web-server))
+ (uri
+ (build-uri 'http #:host "127.0.0.1" #:port port)))
+
+ (assert-equal
+ 200
+ (response-code
+ (http-get
+ uri
+ #:port (nonblocking-open-socket-for-uri uri)))))))
+
+(display "web-server test finished successfully\n")
diff --git a/tests/worker-threads.scm b/tests/worker-threads.scm
new file mode 100644
index 0000000..72a56dc
--- /dev/null
+++ b/tests/worker-threads.scm
@@ -0,0 +1,32 @@
+(use-modules (tests)
+ (srfi srfi-71)
+ (fibers)
+ (unit-test)
+ (knots worker-threads))
+
+(let ((worker-thread-channel
+ (make-worker-thread-channel
+ (const '())
+ #:parallelism 2)))
+
+ (run-fibers-for-tests
+ (lambda ()
+ (assert-equal
+ (call-with-worker-thread
+ worker-thread-channel
+ (lambda ()
+ 4))
+ 4))))
+
+(let ((process-job
+ count-jobs
+ count-threads
+ list-jobs
+ (create-work-queue
+ 2
+ (lambda (i)
+ (* i 2)))))
+
+ (process-job 3))
+
+(display "worker-threads test finished successfully\n")