|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "io" |
| 6 | + "net" |
| 7 | + "os/exec" |
| 8 | + "sync" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestLogdyE2E_Forward(t *testing.T) { |
| 16 | + // Track received messages |
| 17 | + msgReceived := []string{} |
| 18 | + |
| 19 | + // Setup wait groups for coordination |
| 20 | + wg := sync.WaitGroup{} |
| 21 | + wgServer := sync.WaitGroup{} |
| 22 | + wg.Add(3) // Expect 3 messages |
| 23 | + wgServer.Add(1) |
| 24 | + |
| 25 | + // Start TCP server |
| 26 | + go func() { |
| 27 | + l, err := net.Listen("tcp", ":8475") |
| 28 | + if err != nil { |
| 29 | + panic(err) |
| 30 | + } |
| 31 | + defer l.Close() |
| 32 | + wgServer.Done() |
| 33 | + |
| 34 | + conn, err := l.Accept() |
| 35 | + if err != nil { |
| 36 | + panic(err) |
| 37 | + } |
| 38 | + defer conn.Close() |
| 39 | + |
| 40 | + scanner := bufio.NewScanner(conn) |
| 41 | + for scanner.Scan() { |
| 42 | + msgReceived = append(msgReceived, scanner.Text()) |
| 43 | + wg.Done() |
| 44 | + } |
| 45 | + }() |
| 46 | + |
| 47 | + // Wait for server to be ready |
| 48 | + wgServer.Wait() |
| 49 | + |
| 50 | + // Start logdy process |
| 51 | + cmd := exec.Command("go", "run", "../main.go", "forward", "8475") |
| 52 | + |
| 53 | + // Get stdin pipe |
| 54 | + stdin, err := cmd.StdinPipe() |
| 55 | + assert.NoError(t, err) |
| 56 | + |
| 57 | + // Get stdout pipe for logging |
| 58 | + stdout, err := cmd.StdoutPipe() |
| 59 | + assert.NoError(t, err) |
| 60 | + |
| 61 | + // Start reading stdout in background |
| 62 | + go func() { |
| 63 | + scanner := bufio.NewScanner(stdout) |
| 64 | + for scanner.Scan() { |
| 65 | + // Just consume stdout to prevent blocking |
| 66 | + _ = scanner.Text() |
| 67 | + } |
| 68 | + }() |
| 69 | + |
| 70 | + // Start the process |
| 71 | + err = cmd.Start() |
| 72 | + assert.NoError(t, err) |
| 73 | + |
| 74 | + // Give the process a moment to start up |
| 75 | + time.Sleep(1 * time.Second) |
| 76 | + |
| 77 | + // Write test data to stdin |
| 78 | + testLines := []string{ |
| 79 | + "test line 1", |
| 80 | + "test line 2", |
| 81 | + "test line 3", |
| 82 | + } |
| 83 | + |
| 84 | + for _, line := range testLines { |
| 85 | + _, err := io.WriteString(stdin, line+"\n") |
| 86 | + assert.NoError(t, err) |
| 87 | + } |
| 88 | + |
| 89 | + // Wait with timeout |
| 90 | + done := make(chan struct{}) |
| 91 | + go func() { |
| 92 | + wg.Wait() |
| 93 | + close(done) |
| 94 | + }() |
| 95 | + |
| 96 | + select { |
| 97 | + case <-done: |
| 98 | + // Success - all messages received |
| 99 | + case <-time.After(5 * time.Second): |
| 100 | + t.Fatal("Timeout waiting for messages") |
| 101 | + } |
| 102 | + |
| 103 | + // Kill the process |
| 104 | + if err := cmd.Process.Kill(); err != nil { |
| 105 | + t.Errorf("Failed to kill process: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + // Verify received messages |
| 109 | + assert.Equal(t, len(testLines), len(msgReceived)) |
| 110 | + for i, testLine := range testLines { |
| 111 | + assert.Equal(t, testLine, msgReceived[i]) |
| 112 | + } |
| 113 | +} |
0 commit comments