|
1 | 1 | package transport_test |
2 | 2 |
|
3 | 3 | import ( |
4 | | - transport_module "github.com/HyperloopUPV-H8/h9-backend/pkg/transport" |
5 | | - "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/tcp" |
6 | | - "github.com/rs/zerolog" |
| 4 | + "context" |
7 | 5 | "net" |
8 | 6 | "os" |
| 7 | + "sync" |
9 | 8 | "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + transport_module "github.com/HyperloopUPV-H8/h9-backend/pkg/transport" |
| 12 | + "github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/tcp" |
| 13 | + "github.com/rs/zerolog" |
10 | 14 | ) |
11 | 15 |
|
12 | 16 | func TestTransport(t *testing.T) { |
13 | | - // Create server |
14 | 17 | logger := zerolog.New(os.Stdout) |
15 | 18 | transport := transport_module.NewTransport(logger) |
16 | | - err := transport.HandleServer(tcp.NewServerConfig(), "127.0.0.1:8080") |
17 | | - if err != nil { |
18 | | - t.Errorf("Error creating server at 127.0.0.1:8080: %s", err) |
19 | | - } |
20 | 19 |
|
21 | | - // Create client |
| 20 | + // Create a context that cancels after a timeout |
| 21 | + ctx, cancel := context.WithTimeout(context.Background(), 12*time.Millisecond) |
| 22 | + defer cancel() |
| 23 | + |
| 24 | + var wg sync.WaitGroup |
| 25 | + |
| 26 | + wg.Add(1) |
| 27 | + go func() { |
| 28 | + defer wg.Done() |
| 29 | + err := transport.HandleServer(tcp.NewServerConfig(), "127.0.0.1:8080") |
| 30 | + if err != nil { |
| 31 | + t.Errorf("Error creating server at 127.0.0.1:8080: %s", err) |
| 32 | + } |
| 33 | + }() |
| 34 | + |
| 35 | + time.Sleep(10 * time.Millisecond) |
| 36 | + |
| 37 | + // Simulate client interaction |
22 | 38 | addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:3000") |
23 | | - err = transport.HandleClient(tcp.NewClientConfig(addr), "127.0.0.1:8080") |
24 | | - if err != nil { |
25 | | - t.Errorf("Error creating client at 127.0.0.1:3000: %s", err) |
26 | | - } |
| 39 | + wg.Add(1) |
| 40 | + go func() { |
| 41 | + defer wg.Done() |
| 42 | + err := transport.HandleClient(tcp.NewClientConfig(addr), "127.0.0.1:8080") |
| 43 | + if err != nil { |
| 44 | + t.Errorf("Error creating client at 127.0.0.1:3000: %s", err) |
| 45 | + } |
| 46 | + }() |
27 | 47 |
|
28 | 48 | // Create client with wrong address |
29 | 49 | addr, _ = net.ResolveTCPAddr("tcp", "127.0.0.1:3030") |
30 | | - err = transport.HandleClient(tcp.NewClientConfig(addr), "127.0.0.1:8000") |
31 | | - if err == nil { |
32 | | - t.Errorf("Expected error creating client at wrong address, got nil") |
| 50 | + wg.Add(1) |
| 51 | + go func() { |
| 52 | + defer wg.Done() |
| 53 | + err := transport.HandleClient(tcp.NewClientConfig(addr), "127.0.0.1:8000") |
| 54 | + if err == nil { |
| 55 | + t.Errorf("Expected error creating client at wrong address, got nil") |
| 56 | + } |
| 57 | + }() |
| 58 | + |
| 59 | + // Wait for context cancellation or error |
| 60 | + go func() { |
| 61 | + wg.Wait() |
| 62 | + }() |
| 63 | + |
| 64 | + <-ctx.Done() // Wait for timeout or manual cancel |
| 65 | + if ctx.Err() == context.DeadlineExceeded { |
| 66 | + t.Logf("Test completed by timeout") |
33 | 67 | } |
34 | 68 | } |
0 commit comments