-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Piotr
committed
Dec 10, 2024
1 parent
8568e63
commit d05ae20
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package tests | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"net" | ||
"os/exec" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestLogdyE2E_Forward(t *testing.T) { | ||
// Track received messages | ||
msgReceived := []string{} | ||
|
||
// Setup wait groups for coordination | ||
wg := sync.WaitGroup{} | ||
wgServer := sync.WaitGroup{} | ||
wg.Add(3) // Expect 3 messages | ||
wgServer.Add(1) | ||
|
||
// Start TCP server | ||
go func() { | ||
l, err := net.Listen("tcp", ":8475") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer l.Close() | ||
wgServer.Done() | ||
|
||
conn, err := l.Accept() | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer conn.Close() | ||
|
||
scanner := bufio.NewScanner(conn) | ||
for scanner.Scan() { | ||
msgReceived = append(msgReceived, scanner.Text()) | ||
wg.Done() | ||
} | ||
}() | ||
|
||
// Wait for server to be ready | ||
wgServer.Wait() | ||
|
||
// Start logdy process | ||
cmd := exec.Command("go", "run", "../main.go", "forward", "8475") | ||
|
||
// Get stdin pipe | ||
stdin, err := cmd.StdinPipe() | ||
assert.NoError(t, err) | ||
|
||
// Get stdout pipe for logging | ||
stdout, err := cmd.StdoutPipe() | ||
assert.NoError(t, err) | ||
|
||
// Start reading stdout in background | ||
go func() { | ||
scanner := bufio.NewScanner(stdout) | ||
for scanner.Scan() { | ||
// Just consume stdout to prevent blocking | ||
_ = scanner.Text() | ||
} | ||
}() | ||
|
||
// Start the process | ||
err = cmd.Start() | ||
assert.NoError(t, err) | ||
|
||
// Give the process a moment to start up | ||
time.Sleep(1 * time.Second) | ||
|
||
// Write test data to stdin | ||
testLines := []string{ | ||
"test line 1", | ||
"test line 2", | ||
"test line 3", | ||
} | ||
|
||
for _, line := range testLines { | ||
_, err := io.WriteString(stdin, line+"\n") | ||
assert.NoError(t, err) | ||
} | ||
|
||
// Wait with timeout | ||
done := make(chan struct{}) | ||
go func() { | ||
wg.Wait() | ||
close(done) | ||
}() | ||
|
||
select { | ||
case <-done: | ||
// Success - all messages received | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("Timeout waiting for messages") | ||
} | ||
|
||
// Kill the process | ||
if err := cmd.Process.Kill(); err != nil { | ||
t.Errorf("Failed to kill process: %v", err) | ||
} | ||
|
||
// Verify received messages | ||
assert.Equal(t, len(testLines), len(msgReceived)) | ||
for i, testLine := range testLines { | ||
assert.Equal(t, testLine, msgReceived[i]) | ||
} | ||
} |