-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathflake.nix
More file actions
929 lines (757 loc) · 30.6 KB
/
Copy pathflake.nix
File metadata and controls
929 lines (757 loc) · 30.6 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
{
description = "ae library tests";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
libev-binding = pkgs.fetchFromGitHub {
owner = "s-ludwig";
repo = "libev";
rev = "abe87bdbf776ce890ac485fa2e595727e1c223a8";
hash = "sha256-Nc0w4XMENclUt8caK/fIcKAbUM6d4yqp8t4nwHKDDSk=";
};
# Common dub test function
dubTest = { name, subpackage ? null, extraDeps ? [], extraFlags ? [] }:
pkgs.stdenv.mkDerivation {
name = "ae-${name}-test";
nativeBuildInputs = [ pkgs.dub pkgs.ldc pkgs.git ] ++ extraDeps;
dontStrip = true;
# Don't use src = self; we need to set up the directory structure manually
unpackPhase = ''
cp -a ${self} ae
chmod -R u+w ae
cd ae
'';
buildPhase = ''
export HOME="$TMPDIR"
echo "Running ${name} tests..."
dub test \
--compiler=ldc2 \
--debug=ae_unittest \
${if subpackage != null then ":${subpackage}" else ""} \
${builtins.concatStringsSep " " extraFlags}
echo "${name} tests passed!"
'';
installPhase = ''
touch $out
'';
};
# ===========================================
# Test Binaries (for integration tests that need server setup)
# These use ldc2 directly because they need special version flags
# ===========================================
# MySQL test binary - needs HAVE_MYSQL_SERVER version flag
mysql-test-bin = pkgs.stdenv.mkDerivation {
name = "ae-mysql-test-bin";
nativeBuildInputs = [ pkgs.ldc ];
dontStrip = true;
unpackPhase = ''
cp -a ${self} ae
'';
buildPhase = ''
echo "Compiling MySQL client tests..."
# ASOCKETS_DEBUG_IDLE: DO NOT REMOVE - essential for detecting stuck event loops
ldc2 \
-i \
-I. \
-g \
-d-debug=ae_unittest \
-d-debug=ASOCKETS_DEBUG_IDLE \
-d-version=HAVE_MYSQL_SERVER \
-unittest \
--main \
-of=mysql_test \
ae/net/db/mysql/package.d
'';
installPhase = ''
mkdir -p $out/bin
cp mysql_test $out/bin/
'';
};
# PostgreSQL test binary - needs HAVE_PSQL_SERVER version flag
psql-test-bin = pkgs.stdenv.mkDerivation {
name = "ae-psql-test-bin";
nativeBuildInputs = [ pkgs.ldc ];
dontStrip = true;
unpackPhase = ''
cp -a ${self} ae
'';
buildPhase = ''
echo "Compiling PostgreSQL client tests..."
# ASOCKETS_DEBUG_IDLE: DO NOT REMOVE - essential for detecting stuck event loops
ldc2 \
-i \
-I. \
-g \
-d-debug=ae_unittest \
-d-debug=ASOCKETS_DEBUG_IDLE \
-d-version=HAVE_PSQL_SERVER \
-unittest \
--main \
-of=psql_test \
ae/net/db/psql/package.d
'';
installPhase = ''
mkdir -p $out/bin
cp psql_test $out/bin/
'';
};
# WebSocket test binary - needs HAVE_WS_PEER version flag and zlib
websocket-test-bin = pkgs.stdenv.mkDerivation {
name = "ae-websocket-test-bin";
nativeBuildInputs = [ pkgs.ldc pkgs.zlib ];
dontStrip = true;
unpackPhase = ''
cp -a ${self} ae
'';
buildPhase = ''
echo "Compiling WebSocket tests..."
# ASOCKETS_DEBUG_IDLE: DO NOT REMOVE - essential for detecting stuck event loops
ldc2 \
-i \
-I. \
-g \
-d-debug=ae_unittest \
-d-debug=ASOCKETS_DEBUG_IDLE \
-d-version=HAVE_WS_PEER \
-unittest \
--main \
-of=ws_test \
-L=-lz \
ae/net/http/websocket.d
'';
installPhase = ''
mkdir -p $out/bin
cp ws_test $out/bin/
'';
};
# JSON-RPC integration test binary - needs HAVE_JSONRPC_PEER version flag
jsonrpc-test-bin = pkgs.stdenv.mkDerivation {
name = "ae-jsonrpc-test-bin";
nativeBuildInputs = [ pkgs.ldc ];
dontStrip = true;
unpackPhase = ''
cp -a ${self} ae
'';
buildPhase = ''
echo "Compiling JSON-RPC integration tests..."
# ASOCKETS_DEBUG_IDLE: DO NOT REMOVE - essential for detecting stuck event loops
ldc2 \
-i \
-I. \
-g \
-d-debug=ae_unittest \
-d-debug=ASOCKETS_DEBUG_IDLE \
-d-version=HAVE_JSONRPC_PEER \
-unittest \
--main \
-of=jsonrpc_test \
ae/net/jsonrpc/binding.d
'';
installPhase = ''
mkdir -p $out/bin
cp jsonrpc_test $out/bin/
'';
};
# Content-Length framing integration test binary - needs HAVE_CONTENTLENGTH_PEER version flag
contentlength-test-bin = pkgs.stdenv.mkDerivation {
name = "ae-contentlength-test-bin";
nativeBuildInputs = [ pkgs.ldc ];
dontStrip = true;
unpackPhase = ''
cp -a ${self} ae
'';
buildPhase = ''
echo "Compiling Content-Length framing integration tests..."
# ASOCKETS_DEBUG_IDLE: DO NOT REMOVE - essential for detecting stuck event loops
ldc2 \
-i \
-I. \
-g \
-d-debug=ae_unittest \
-d-debug=ASOCKETS_DEBUG_IDLE \
-d-version=HAVE_CONTENTLENGTH_PEER \
-unittest \
--main \
-of=contentlength_test \
ae/net/jsonrpc/contentlength.d
'';
installPhase = ''
mkdir -p $out/bin
cp contentlength_test $out/bin/
'';
};
# D-Bus test binary - needs HAVE_DBUS_SERVER version flag
dbus-test-bin = pkgs.stdenv.mkDerivation {
name = "ae-dbus-test-bin";
nativeBuildInputs = [ pkgs.ldc ];
dontStrip = true;
unpackPhase = ''
cp -a ${self} ae
'';
buildPhase = ''
echo "Compiling D-Bus client tests..."
# ASOCKETS_DEBUG_IDLE: DO NOT REMOVE - essential for detecting stuck event loops
ldc2 \
-i \
-I. \
-g \
-d-debug=ae_unittest \
-d-debug=ASOCKETS_DEBUG_IDLE \
-d-version=HAVE_DBUS_SERVER \
-unittest \
--main \
-of=dbus_test \
ae/net/dbus/package.d
'';
installPhase = ''
mkdir -p $out/bin
cp dbus_test $out/bin/
'';
};
libev-test-bin = pkgs.stdenv.mkDerivation {
name = "ae-libev-test-bin";
nativeBuildInputs = [ pkgs.ldc pkgs.libev ];
dontStrip = true;
unpackPhase = ''
cp -a ${self} ae
'';
buildPhase = ''
echo "Compiling libev backend tests..."
# ASOCKETS_DEBUG_IDLE: DO NOT REMOVE - essential for detecting stuck event loops
ldc2 \
-i \
-I. \
-I${libev-binding} \
-g \
-d-version=LIBEV \
-d-debug=ae_unittest \
-d-debug=ASOCKETS_DEBUG_IDLE \
-unittest \
--main \
-L=-lev \
-of=libev_test \
ae/net/asockets.d
'';
installPhase = ''
mkdir -p $out/bin
cp libev_test $out/bin/
'';
};
in {
packages = {
inherit mysql-test-bin psql-test-bin websocket-test-bin jsonrpc-test-bin contentlength-test-bin;
} // pkgs.lib.optionalAttrs pkgs.stdenv.isLinux {
inherit dbus-test-bin;
};
checks = {
# libev backend runtime tests
libev = pkgs.stdenv.mkDerivation {
name = "ae-libev-test";
nativeBuildInputs = [ libev-test-bin ];
dontUnpack = true;
buildPhase = ''
export HOME="$TMPDIR"
libev_test
'';
installPhase = ''
touch $out
'';
};
# ===========================================
# Unit Tests (using dub test)
# ===========================================
# Main library unit tests
main = dubTest {
name = "main";
};
# SQLite subpackage unit tests
sqlite = dubTest {
name = "sqlite";
subpackage = "sqlite";
extraDeps = [ pkgs.sqlite ];
};
# zlib subpackage unit tests
zlib = dubTest {
name = "zlib";
subpackage = "zlib";
extraDeps = [ pkgs.zlib ];
};
# ===========================================
# rdmd link test (regression guard for D issue 7016)
# ===========================================
# Regression guard for https://issues.dlang.org/show_bug.cgi?id=7016:
# rdmd finds a program's dependencies by scanning the compiler's `-v`
# output, which omits imports that appear only inside a function body.
# So if a module uses non-template symbols (a class, a plain function,
# a module ctor) of another module it imports only inside a function,
# rdmd never compiles that module and downstream rdmd projects fail to
# link.
#
# We build, with rdmd, a one-line program importing each main-package
# module on its own — what a downstream project does — and let the rdmd
# exit code decide. Each module must be imported in isolation:
# importing several at once would let one module's top-level import
# pull in another module's function-locally-imported victim and mask
# the bug, so the builds cannot be collapsed into one. They instead run
# in parallel (NIX_BUILD_CORES), each job in its own temp dir.
#
# The module list comes from `dub describe`, so the external-dependency
# / platform exclusions in dub.sdl (openssl, sqlite, X11, …) are
# honoured and the rest link with no external libraries; thus a non-zero
# exit is a real failure, not a missing -l flag. Files whose name
# differs from their `module` (e.g. utils/math/comparison_.d) are
# skipped: dub compiles them by path, but they cannot be imported alone.
#
# rdmd is built from the dtools source with ldc, since the nixpkgs `dmd`
# package is not always cached and `ldc` ships no `rdmd`; ldc shares
# dmd's front end, so it reproduces the bug.
rdmd =
let
# Build one isolated rdmd program (used as an xargs worker). Its
# own temp dir doubles as rdmd's cache dir, so parallel jobs don't
# race; a non-zero exit propagates through xargs to fail the build.
buildOne = pkgs.writeShellScript "ae-rdmd-build-one" ''
set -e
mod=$1
work="$TMPDIR/by-module/$mod"
mkdir -p "$work"
export TMPDIR="$work"
printf 'import %s;\nvoid main(){}\n' "$mod" > "$work/probe.d"
exec "$RDMD" --compiler=ldmd2 -I"$BUILDROOT" \
-od="$work" --build-only -of="$work/bin" "$work/probe.d"
'';
in pkgs.stdenv.mkDerivation {
name = "ae-rdmd-link-test";
nativeBuildInputs = [ pkgs.ldc pkgs.dub ];
dontStrip = true;
unpackPhase = ''
cp -a ${self} ae
chmod -R u+w ae
'';
buildPhase = ''
export HOME="$TMPDIR"
echo "Building rdmd with ldc..."
ldmd2 -of="$TMPDIR/rdmd" ${pkgs.dtools.src}/rdmd.d
export RDMD="$TMPDIR/rdmd"
export BUILDROOT="$PWD" # contains ./ae, the import root
cd ae
# Collect the modules to test, one per line.
for f in $(dub describe --compiler=ldc2 --data=source-files --data-list \
| sed "s|^$PWD/||" | grep '\.d$'); do
modpath="ae.''${f%.d}"; modpath="''${modpath//\//.}"; modpath="''${modpath%.package}"
# Module name from the declaration (handles `deprecated module …;`).
moddecl=$(grep -m1 -oE 'module[[:space:]]+[A-Za-z0-9_.]+[[:space:]]*;' "$f" \
| sed -E 's/module[[:space:]]+([A-Za-z0-9_.]+).*/\1/')
[ "$modpath" = "$moddecl" ] && echo "$modpath"
done > "$TMPDIR/modules.txt"
[ -s "$TMPDIR/modules.txt" ] # dub describe produced something
cores=''${NIX_BUILD_CORES:-0}; [ "$cores" -gt 0 ] || cores=$(nproc)
echo "Linking $(wc -l < "$TMPDIR/modules.txt") modules with rdmd ($cores in parallel)..."
xargs -P "$cores" -n1 ${buildOne} < "$TMPDIR/modules.txt"
echo "rdmd link test passed."
'';
installPhase = ''
touch $out
'';
};
# ===========================================
# Integration Tests (with database servers)
# ===========================================
# MySQL/MariaDB integration tests
mysql = pkgs.stdenv.mkDerivation {
name = "ae-mysql-test";
src = self;
nativeBuildInputs = [ pkgs.mariadb mysql-test-bin ];
buildPhase = ''
export HOME="$TMPDIR"
export MYSQL_HOME="$TMPDIR"
export MYSQL_DATADIR="$TMPDIR/mysql"
export MYSQL_UNIX_PORT="$TMPDIR/mysql.sock"
export MYSQL_HOST="127.0.0.1"
export MYSQL_TCP_PORT="3306"
export MYSQL_USER="testuser"
export MYSQL_PWD="testpass"
export MYSQL_DATABASE="testdb"
echo "Initializing MariaDB..."
mysql_install_db --datadir="$MYSQL_DATADIR" --auth-root-authentication-method=normal
echo "Starting MariaDB..."
mysqld --datadir="$MYSQL_DATADIR" --socket="$MYSQL_UNIX_PORT" --port="$MYSQL_TCP_PORT" --skip-networking=0 --bind-address=127.0.0.1 &
MYSQL_PID=$!
# Wait for server to start
for i in $(seq 1 30); do
if mysqladmin --socket="$MYSQL_UNIX_PORT" ping >/dev/null 2>&1; then
break
fi
sleep 1
done
echo "Creating test user and database..."
MYSQL_PWD= mysql --socket="$MYSQL_UNIX_PORT" -u root <<EOF
CREATE USER '$MYSQL_USER'@'localhost' IDENTIFIED BY '$MYSQL_PWD';
CREATE USER '$MYSQL_USER'@'127.0.0.1' IDENTIFIED BY '$MYSQL_PWD';
CREATE DATABASE $MYSQL_DATABASE;
GRANT ALL ON $MYSQL_DATABASE.* TO '$MYSQL_USER'@'localhost';
GRANT ALL ON $MYSQL_DATABASE.* TO '$MYSQL_USER'@'127.0.0.1';
FLUSH PRIVILEGES;
EOF
echo "Testing TCP connectivity to MariaDB..."
mysql -h 127.0.0.1 -P 3306 -u "$MYSQL_USER" -p"$MYSQL_PWD" -e "SELECT 1" "$MYSQL_DATABASE"
echo "Running MySQL client tests..."
mysql_test
echo "Stopping MariaDB..."
kill $MYSQL_PID || true
wait $MYSQL_PID || true
echo "MySQL tests passed!"
'';
installPhase = ''
touch $out
'';
};
# PostgreSQL integration tests
psql = pkgs.stdenv.mkDerivation {
name = "ae-psql-test";
src = self;
nativeBuildInputs = [ pkgs.postgresql psql-test-bin ];
buildPhase = ''
export HOME="$TMPDIR"
export PGDATA="$TMPDIR/pgdata"
export PGHOST="$TMPDIR"
export PGUSER="testuser"
export PGPASSWORD="testpass"
export PGDATABASE="testdb"
echo "Initializing PostgreSQL..."
initdb -D "$PGDATA" --auth=trust --username=postgres
cat >> "$PGDATA/postgresql.conf" <<EOF
unix_socket_directories = '$TMPDIR'
listen_addresses = 'localhost'
port = 5432
password_encryption = scram-sha-256
EOF
cat > "$PGDATA/pg_hba.conf" <<EOF
# TYPE DATABASE USER ADDRESS METHOD
local all postgres trust
local all all scram-sha-256
host all all 127.0.0.1/32 scram-sha-256
host all all ::1/128 scram-sha-256
EOF
echo "Starting PostgreSQL..."
pg_ctl -D "$PGDATA" -l "$PGDATA/logfile" start -w
echo "Creating test user and database with SCRAM-SHA-256..."
psql -h "$TMPDIR" -U postgres -d postgres -c "CREATE USER $PGUSER WITH PASSWORD '$PGPASSWORD';"
psql -h "$TMPDIR" -U postgres -d postgres -c "CREATE DATABASE $PGDATABASE OWNER $PGUSER;"
export PGHOST="localhost"
echo "Running PostgreSQL client tests..."
psql_test
echo "Stopping PostgreSQL..."
pg_ctl -D "$PGDATA" stop -m fast
echo "PostgreSQL tests passed!"
'';
installPhase = ''
touch $out
'';
};
# WebSocket integration tests (with Python websockets peer)
websocket = pkgs.stdenv.mkDerivation {
name = "ae-websocket-test";
src = self;
nativeBuildInputs = [
websocket-test-bin
(pkgs.python3.withPackages (ps: [ ps.websockets ]))
];
buildPhase = ''
export HOME="$TMPDIR"
echo "=== Phase 1: D client with Python server ==="
# Start Python WebSocket echo server (permessage-deflate enabled by default)
export WS_READY_FILE="$TMPDIR/py_ws1_ready"
python3 << 'PYEOF' &
import asyncio, websockets, pathlib, os
async def echo(ws):
async for msg in ws:
await ws.send(msg)
async def main():
async with websockets.serve(echo, "127.0.0.1", 18765):
pathlib.Path(os.environ["WS_READY_FILE"]).write_text("ready")
await asyncio.Future()
asyncio.run(main())
PYEOF
PY_PID=$!
for i in $(seq 1 30); do
if [ -f "$WS_READY_FILE" ]; then break; fi
sleep 0.5
done
echo "Python server ready, running D client test..."
WS_TEST_MODE=client WS_SERVER_PORT=18765 ws_test
echo "D client test passed!"
if ! kill $PY_PID 2>/dev/null; then
wait $PY_PID
echo "FAIL: Python WebSocket server (phase 1) exited prematurely (exit code: $?)"
exit 1
fi
wait $PY_PID 2>/dev/null || true
echo "=== Phase 2: D server with Python client ==="
# Start D WebSocket echo server
WS_TEST_MODE=server WS_PORT=18766 WS_READY_FILE="$TMPDIR/ws_ready" ws_test &
D_PID=$!
# Wait for D server to be ready
for i in $(seq 1 30); do
if [ -f "$TMPDIR/ws_ready" ]; then
break
fi
sleep 0.5
done
echo "D server ready, running Python client test..."
python3 << 'PYEOF'
import asyncio, websockets
async def main():
async with websockets.connect("ws://127.0.0.1:18766") as ws:
# Verify permessage-deflate was negotiated
ext_names = [e.name for e in ws.protocol.extensions]
assert "permessage-deflate" in ext_names, f"Expected permessage-deflate, got: {ext_names}"
await ws.send("Hello from Python client")
response = await ws.recv()
assert response == b"Hello from Python client", f"Expected echo, got: {response!r}"
print("Python client: echo verified with compression!")
asyncio.run(main())
PYEOF
echo "Python client test passed!"
# Wait for D server to exit (closes after client disconnects)
wait $D_PID
echo "=== Phase 3: D client with Python server (no_context_takeover) ==="
# Start Python echo server with no_context_takeover
export WS_READY_FILE="$TMPDIR/py_ws3_ready"
python3 << 'PYEOF' &
import asyncio, websockets, pathlib, os
from websockets.extensions.permessage_deflate import ServerPerMessageDeflateFactory
async def echo(ws):
async for msg in ws:
await ws.send(msg)
async def main():
async with websockets.serve(
echo, "127.0.0.1", 18768,
extensions=[ServerPerMessageDeflateFactory(
server_no_context_takeover=True,
client_no_context_takeover=True,
)],
):
pathlib.Path(os.environ["WS_READY_FILE"]).write_text("ready")
await asyncio.Future()
asyncio.run(main())
PYEOF
PY_PID=$!
for i in $(seq 1 30); do
if [ -f "$WS_READY_FILE" ]; then break; fi
sleep 0.5
done
echo "Python server (no_context_takeover) ready, running D client test..."
WS_TEST_MODE=client_nctx WS_SERVER_PORT=18768 ws_test
echo "D client no_context_takeover test passed!"
if ! kill $PY_PID 2>/dev/null; then
wait $PY_PID
echo "FAIL: Python WebSocket server (phase 3) exited prematurely (exit code: $?)"
exit 1
fi
wait $PY_PID 2>/dev/null || true
echo "=== Phase 4: D server with Python client (no_context_takeover) ==="
WS_TEST_MODE=server_nctx WS_PORT=18767 WS_READY_FILE="$TMPDIR/ws_ready_nctx" ws_test &
D_PID=$!
for i in $(seq 1 30); do
if [ -f "$TMPDIR/ws_ready_nctx" ]; then
break
fi
sleep 0.5
done
echo "D server ready, running Python client (no_context_takeover) test..."
python3 << 'PYEOF'
import asyncio, websockets
from websockets.extensions.permessage_deflate import ClientPerMessageDeflateFactory
async def main():
async with websockets.connect(
"ws://127.0.0.1:18767",
extensions=[ClientPerMessageDeflateFactory(
server_no_context_takeover=True,
client_no_context_takeover=True,
)],
) as ws:
ext_names = [e.name for e in ws.protocol.extensions]
assert "permessage-deflate" in ext_names, f"Expected permessage-deflate, got: {ext_names}"
messages = ["Message one", "Message two", "Message three"]
for msg in messages:
await ws.send(msg)
response = await ws.recv()
assert response == msg.encode(), f"Expected {msg!r}, got: {response!r}"
print("Python client: no_context_takeover echo verified!")
asyncio.run(main())
PYEOF
echo "Python client no_context_takeover test passed!"
wait $D_PID
echo "All WebSocket integration tests passed!"
'';
installPhase = ''
touch $out
'';
};
# JSON-RPC integration tests (with Python jsonrpclib-pelix peer)
jsonrpc = pkgs.stdenv.mkDerivation {
name = "ae-jsonrpc-test";
src = self;
nativeBuildInputs = [
jsonrpc-test-bin
(pkgs.python3.withPackages (ps: [ ps.jsonrpclib-pelix ]))
];
buildPhase = ''
export HOME="$TMPDIR"
echo "=== Phase 1: D server with Python client ==="
# Start D JSON-RPC server; it writes port to ready file when listening
JSONRPC_TEST_MODE=server JSONRPC_READY_FILE="$TMPDIR/jsonrpc_ready" jsonrpc_test &
D_PID=$!
# Wait for D server to signal readiness (port written to file)
for i in $(seq 1 30); do
[ -f "$TMPDIR/jsonrpc_ready" ] && break; sleep 0.5
done
PORT=$(cat "$TMPDIR/jsonrpc_ready")
python3 << PYEOF
import jsonrpclib
proxy = jsonrpclib.Server("http://127.0.0.1:$PORT")
result = proxy.add(2, 3)
assert result == 5, "positional add(2,3) failed: " + str(result)
result = proxy.add(a=10, b=7)
assert result == 17, "named add(a=10,b=7) failed: " + str(result)
result = proxy.addVec(x=3, y=4)
assert result == 7, "addVec(x=3,y=4) failed: " + str(result)
print("Phase 1: all assertions passed!")
PYEOF
kill $D_PID || true
wait $D_PID 2>/dev/null || true
echo "=== Phase 2: Python server with D client ==="
# Start Python JSON-RPC server; capture its port from stdout
python3 << 'PYEOF' > "$TMPDIR/py_server_port" &
from jsonrpclib.SimpleJSONRPCServer import SimpleJSONRPCServer
server = SimpleJSONRPCServer(("127.0.0.1", 0), logRequests=False)
server.register_function(lambda a, b: a + b, "add")
print(server.server_address[1], flush=True)
server.serve_forever()
PYEOF
PY_PID=$!
# Wait for Python server to print its port
for i in $(seq 1 30); do
[ -s "$TMPDIR/py_server_port" ] && break; sleep 0.5
done
PY_PORT=$(cat "$TMPDIR/py_server_port")
JSONRPC_TEST_MODE=client JSONRPC_SERVER_PORT="$PY_PORT" jsonrpc_test
kill $PY_PID || true
wait $PY_PID 2>/dev/null || true
echo "All JSON-RPC integration tests passed!"
'';
installPhase = ''
touch $out
'';
};
# Content-Length framing integration tests (with Python python-lsp-jsonrpc peer)
contentlength = pkgs.stdenv.mkDerivation {
name = "ae-contentlength-test";
src = self;
nativeBuildInputs = [
contentlength-test-bin
(pkgs.python3.withPackages (ps: [ ps.python-lsp-jsonrpc ]))
];
buildPhase = ''
export HOME="$TMPDIR"
echo "=== Phase 1: D server with Python client ==="
# Start D JSON-RPC server with Content-Length framing over TCP
CL_TEST_MODE=server CL_READY_FILE="$TMPDIR/cl_ready" contentlength_test &
D_PID=$!
# Wait for D server to signal readiness (port written to file)
for i in $(seq 1 30); do
[ -f "$TMPDIR/cl_ready" ] && break; sleep 0.5
done
PORT=$(cat "$TMPDIR/cl_ready")
python3 << PYEOF
import socket, threading
from pylsp_jsonrpc.streams import JsonRpcStreamReader, JsonRpcStreamWriter
from pylsp_jsonrpc.endpoint import Endpoint
sock = socket.socket()
sock.connect(("127.0.0.1", $PORT))
rfile = sock.makefile("rb")
wfile = sock.makefile("wb")
writer = JsonRpcStreamWriter(wfile)
endpoint = Endpoint({}, writer.write)
reader = JsonRpcStreamReader(rfile)
t = threading.Thread(target=reader.listen, args=(endpoint.consume,), daemon=True)
t.start()
f1 = endpoint.request("add", [2, 3])
assert f1.result(timeout=5) == 5, f"Expected 5, got {f1.result()}"
f2 = endpoint.request("add", [10, 7])
assert f2.result(timeout=5) == 17, f"Expected 17, got {f2.result()}"
print("Phase 1: all assertions passed!")
sock.close()
PYEOF
kill $D_PID || true
wait $D_PID 2>/dev/null || true
echo "=== Phase 2: Python server with D client ==="
# Start Python JSON-RPC server with Content-Length framing over TCP
python3 << 'PYEOF' > "$TMPDIR/py_server_port" &
import socket, threading
from pylsp_jsonrpc.streams import JsonRpcStreamReader, JsonRpcStreamWriter
from pylsp_jsonrpc.endpoint import Endpoint
sock = socket.socket()
sock.bind(("127.0.0.1", 0))
sock.listen(1)
print(sock.getsockname()[1], flush=True)
conn, _ = sock.accept()
rfile = conn.makefile("rb")
wfile = conn.makefile("wb")
writer = JsonRpcStreamWriter(wfile)
def add_handler(params):
return params[0] + params[1]
endpoint = Endpoint({"add": add_handler}, writer.write)
reader = JsonRpcStreamReader(rfile)
reader.listen(endpoint.consume)
PYEOF
PY_PID=$!
# Wait for Python server to print its port
for i in $(seq 1 30); do
[ -s "$TMPDIR/py_server_port" ] && break; sleep 0.5
done
PY_PORT=$(cat "$TMPDIR/py_server_port")
CL_TEST_MODE=client CL_SERVER_PORT="$PY_PORT" contentlength_test
kill $PY_PID || true
wait $PY_PID 2>/dev/null || true
echo "All Content-Length framing integration tests passed!"
'';
installPhase = ''
touch $out
'';
};
} // pkgs.lib.optionalAttrs pkgs.stdenv.isLinux {
# D-Bus integration tests, against an isolated packaged dbus-daemon
dbus = pkgs.stdenv.mkDerivation {
name = "ae-dbus-test";
nativeBuildInputs = [ pkgs.dbus dbus-test-bin ];
dontUnpack = true;
buildPhase = ''
export HOME="$TMPDIR"
${pkgs.dbus}/bin/dbus-run-session --config-file=${pkgs.dbus}/share/dbus-1/session.conf -- dbus_test
'';
installPhase = ''
touch $out
'';
};
};
devShells.default = pkgs.mkShell {
buildInputs = [
pkgs.dub
pkgs.ldc
pkgs.git
pkgs.postgresql
pkgs.mariadb
pkgs.sqlite
pkgs.zlib
];
};
}
);
}