|
| 1 | +package daemon_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/kapetan-io/querator/daemon" |
| 7 | + pb "github.com/kapetan-io/querator/proto" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "io" |
| 11 | + "net" |
| 12 | + "net/http" |
| 13 | + "strings" |
| 14 | + "sync" |
| 15 | + "testing" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +func TestInMemoryListener(t *testing.T) { |
| 20 | + listener := daemon.NewInMemoryListener() |
| 21 | + server := &http.Server{ |
| 22 | + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 | + _, _ = fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) |
| 24 | + }), |
| 25 | + } |
| 26 | + go func() { _ = server.Serve(listener) }() |
| 27 | + defer func() { _ = server.Close() }() |
| 28 | + |
| 29 | + clientCount := 3 |
| 30 | + var wg sync.WaitGroup |
| 31 | + for i := 0; i < clientCount; i++ { |
| 32 | + wg.Add(1) |
| 33 | + go func(id int) { |
| 34 | + defer wg.Done() |
| 35 | + // Each client gets its own net.Pipe |
| 36 | + serverConn, clientConn := net.Pipe() |
| 37 | + _ = listener.ServeConn(serverConn) |
| 38 | + |
| 39 | + // Custom DialContext returns the clientConn for this request |
| 40 | + dialOnce := sync.Once{} |
| 41 | + |
| 42 | + client := &http.Client{ |
| 43 | + Transport: &http.Transport{ |
| 44 | + DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { |
| 45 | + var c net.Conn |
| 46 | + dialOnce.Do(func() { c = clientConn }) |
| 47 | + return c, nil |
| 48 | + }, |
| 49 | + }, |
| 50 | + Timeout: 2 * time.Second, |
| 51 | + } |
| 52 | + |
| 53 | + url := fmt.Sprintf("http://inmemory/client%d", id) |
| 54 | + resp, err := client.Get(url) |
| 55 | + if err != nil { |
| 56 | + t.Errorf("client %d error: %v", id, err) |
| 57 | + return |
| 58 | + } |
| 59 | + defer func() { _ = resp.Body.Close() }() |
| 60 | + body, _ := io.ReadAll(resp.Body) |
| 61 | + expected := fmt.Sprintf("Hello, client%d!", id) |
| 62 | + if !strings.Contains(string(body), expected) { |
| 63 | + t.Errorf("client %d got unexpected body: %q", id, body) |
| 64 | + } |
| 65 | + }(i) |
| 66 | + } |
| 67 | + wg.Wait() |
| 68 | + _ = listener.Close() |
| 69 | +} |
| 70 | + |
| 71 | +func TestDaemonInMemoryListener(t *testing.T) { |
| 72 | + ctx := context.Background() |
| 73 | + |
| 74 | + // Create daemon with InMemoryListener enabled |
| 75 | + d, err := daemon.NewDaemon(ctx, daemon.Config{ |
| 76 | + InMemoryListener: true, |
| 77 | + }) |
| 78 | + require.NoError(t, err) |
| 79 | + defer func() { _ = d.Shutdown(ctx) }() |
| 80 | + |
| 81 | + // Get a client - should create a new net.Pipe connection |
| 82 | + { |
| 83 | + client, err := d.Client() |
| 84 | + require.NoError(t, err) |
| 85 | + |
| 86 | + // Test QueuesList to verify the connection works |
| 87 | + var resp pb.QueuesListResponse |
| 88 | + err = client.QueuesList(ctx, &resp, nil) |
| 89 | + require.NoError(t, err) |
| 90 | + |
| 91 | + // Verify we got an empty list |
| 92 | + assert.Nil(t, resp.Items) |
| 93 | + |
| 94 | + // Should work a second time also |
| 95 | + err = client.QueuesList(ctx, &resp, nil) |
| 96 | + require.NoError(t, err) |
| 97 | + |
| 98 | + // Verify we got an empty list |
| 99 | + assert.Nil(t, resp.Items) |
| 100 | + } |
| 101 | + |
| 102 | + // Test getting multiple clients - each should work independently |
| 103 | + { |
| 104 | + client, err := d.Client() |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + // Test QueuesList to verify the connection works |
| 108 | + var resp pb.QueuesListResponse |
| 109 | + err = client.QueuesList(ctx, &resp, nil) |
| 110 | + require.NoError(t, err) |
| 111 | + |
| 112 | + // Verify we got an empty list |
| 113 | + assert.Nil(t, resp.Items) |
| 114 | + } |
| 115 | +} |
0 commit comments