|
1 | 1 | package king_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "sync" |
5 | 6 | "testing" |
| 7 | + "time" |
6 | 8 |
|
7 | 9 | "github.com/firecow/burrow/internal/king" |
8 | 10 | "github.com/firecow/burrow/internal/state" |
@@ -894,6 +896,84 @@ func TestOnStateChanged_ClearsServicesOnEmpty(t *testing.T) { |
894 | 896 | } |
895 | 897 | } |
896 | 898 |
|
| 899 | +// --- WaitForDrain --- |
| 900 | + |
| 901 | +func TestWaitForDrain_EmptyImmediate(t *testing.T) { |
| 902 | + t.Parallel() |
| 903 | + |
| 904 | + tunnelSrv := king.NewTunnelServer(testBindPort, nil) |
| 905 | + |
| 906 | + done := make(chan struct{}) |
| 907 | + |
| 908 | + go func() { |
| 909 | + tunnelSrv.WaitForDrain(t.Context()) |
| 910 | + close(done) |
| 911 | + }() |
| 912 | + |
| 913 | + select { |
| 914 | + case <-done: |
| 915 | + case <-time.After(time.Second): |
| 916 | + t.Fatal("waitForDrain should return immediately when no connections") |
| 917 | + } |
| 918 | +} |
| 919 | + |
| 920 | +func TestWaitForDrain_WaitsForRemoval(t *testing.T) { |
| 921 | + t.Parallel() |
| 922 | + |
| 923 | + tunnelSrv := king.NewTunnelServer(testBindPort, nil) |
| 924 | + tunnelSrv.SetQUICConn("svc-1", nil) |
| 925 | + |
| 926 | + done := make(chan struct{}) |
| 927 | + |
| 928 | + go func() { |
| 929 | + tunnelSrv.WaitForDrain(t.Context()) |
| 930 | + close(done) |
| 931 | + }() |
| 932 | + |
| 933 | + select { |
| 934 | + case <-done: |
| 935 | + t.Fatal("waitForDrain should not return while connections exist") |
| 936 | + case <-time.After(50 * time.Millisecond): |
| 937 | + } |
| 938 | + |
| 939 | + tunnelSrv.RemoveQUICConn("svc-1") |
| 940 | + |
| 941 | + select { |
| 942 | + case <-done: |
| 943 | + case <-time.After(time.Second): |
| 944 | + t.Fatal("waitForDrain should return after connection removed") |
| 945 | + } |
| 946 | +} |
| 947 | + |
| 948 | +func TestWaitForDrain_RespectsContextTimeout(t *testing.T) { |
| 949 | + t.Parallel() |
| 950 | + |
| 951 | + tunnelSrv := king.NewTunnelServer(testBindPort, nil) |
| 952 | + tunnelSrv.SetQUICConn("svc-1", nil) |
| 953 | + |
| 954 | + ctx, cancel := context.WithTimeout( |
| 955 | + t.Context(), 100*time.Millisecond, |
| 956 | + ) |
| 957 | + defer cancel() |
| 958 | + |
| 959 | + done := make(chan struct{}) |
| 960 | + |
| 961 | + go func() { |
| 962 | + tunnelSrv.WaitForDrain(ctx) |
| 963 | + close(done) |
| 964 | + }() |
| 965 | + |
| 966 | + select { |
| 967 | + case <-done: |
| 968 | + case <-time.After(time.Second): |
| 969 | + t.Fatal("waitForDrain should return on context cancellation") |
| 970 | + } |
| 971 | + |
| 972 | + if tunnelSrv.QUICConnCount() != 1 { |
| 973 | + t.Fatal("connection should still exist after timeout") |
| 974 | + } |
| 975 | +} |
| 976 | + |
897 | 977 | // --- Syncer.TriggerSync --- |
898 | 978 |
|
899 | 979 | func TestTriggerSync_SendsNotification(t *testing.T) { |
|
0 commit comments