-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update package, add td2 client tests * Run tests in release workflow * Add checkout * Add mocked frontend dist directory * Add mocked frontend dist directory * More reliable tests * Better client tests * CAdd test for connection failed * Use parser function from station package
- Loading branch information
1 parent
84f82a9
commit 9d1cd07
Showing
8 changed files
with
284 additions
and
8 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
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
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module changeme | ||
module github.com/kacpermalachowski/marshal-controller | ||
|
||
go 1.21 | ||
|
||
|
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,52 @@ | ||
package testserver | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
type TestTCPServer struct { | ||
ReceivedDataChan chan string | ||
} | ||
|
||
func New() *TestTCPServer { | ||
return &TestTCPServer{ | ||
ReceivedDataChan: make(chan string), | ||
} | ||
} | ||
|
||
func (s *TestTCPServer) Start() (string, error) { | ||
l, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
return "", fmt.Errorf("failed to start test server: %s", err) | ||
} | ||
|
||
go func() { | ||
defer l.Close() | ||
|
||
for { | ||
conn, err := l.Accept() | ||
if err != nil { | ||
return | ||
} | ||
|
||
go func(c net.Conn) { | ||
defer c.Close() | ||
|
||
buf := make([]byte, 1024) | ||
n, err := c.Read(buf) | ||
if err != nil { | ||
return | ||
} | ||
|
||
s.ReceivedDataChan <- string(buf[:n]) | ||
}(conn) | ||
} | ||
}() | ||
|
||
return l.Addr().String(), nil | ||
} | ||
|
||
func (s *TestTCPServer) Stop() { | ||
close(s.ReceivedDataChan) | ||
} |
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
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,94 @@ | ||
package station_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/kacpermalachowski/marshal-controller/pkg/station" | ||
) | ||
|
||
func TestParseStationDefinition(t *testing.T) { | ||
tc := []struct { | ||
Name string | ||
Data string | ||
Expected station.Definition | ||
ExpectErr bool | ||
}{ | ||
{ | ||
Name: "No hills", | ||
Data: `hills:`, | ||
ExpectErr: true, | ||
}, | ||
{ | ||
Name: "Single hill and signal", | ||
Data: `hills: | ||
- signal: Tr1`, | ||
Expected: station.Definition{ | ||
Hills: []station.Hill{ | ||
{ | ||
Signal: "Tr1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "Single hill with repeaters", | ||
Data: `hills: | ||
- signal: Tr1 | ||
repeaters: | ||
- Tr2`, | ||
Expected: station.Definition{ | ||
Hills: []station.Hill{ | ||
{ | ||
Signal: "Tr1", | ||
Repeaters: []string{ | ||
"Tr2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "Multiple hill with repeaters", | ||
Data: `hills: | ||
- signal: Tr1 | ||
repeaters: | ||
- Tr2 | ||
- signal: Tr3 | ||
repeaters: | ||
- Tr4`, | ||
Expected: station.Definition{ | ||
Hills: []station.Hill{ | ||
{ | ||
Signal: "Tr1", | ||
Repeaters: []string{ | ||
"Tr2", | ||
}, | ||
}, | ||
{ | ||
Signal: "Tr3", | ||
Repeaters: []string{ | ||
"Tr4", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range tc { | ||
t.Run(c.Name, func(t *testing.T) { | ||
def, err := station.ParseStationDefinition([]byte(c.Data)) | ||
if err != nil && !c.ExpectErr { | ||
t.Errorf("Unexpected error occured: %s", err) | ||
} | ||
if c.ExpectErr && err == nil { | ||
t.Error("Expected error, but no one occured") | ||
} | ||
|
||
if !reflect.DeepEqual(c.Expected, def) { | ||
t.Errorf("Expected %v, but got %v", c.Expected, def) | ||
} | ||
}) | ||
} | ||
} |
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,93 @@ | ||
package td2_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/kacpermalachowski/marshal-controller/internal/testserver" | ||
"github.com/kacpermalachowski/marshal-controller/pkg/td2" | ||
) | ||
|
||
func TestNewClient(t *testing.T) { | ||
ctx := context.Background() | ||
stationHash := "exampleHash" | ||
client := td2.New(ctx, stationHash) | ||
|
||
if client.IsConnected { | ||
t.Error("Expected IsConnected to be false, but got true") | ||
} | ||
|
||
if len(client.ReadChan) != 0 { | ||
t.Error("Expected ReadChan to be empty, but it is not") | ||
} | ||
} | ||
|
||
func TestConnectDisconnect(t *testing.T) { | ||
ctx := context.Background() | ||
stationHash := "exampleHash" | ||
client := td2.New(ctx, stationHash) | ||
|
||
server := testserver.New() | ||
addr, err := server.Start() | ||
if err != nil { | ||
t.Fatalf("Failed to setup test server: %s", err) | ||
} | ||
|
||
// Test Connect | ||
err = client.Connect(addr) | ||
if err != nil { | ||
t.Errorf("Expected no error on Connect, but got: %v", err) | ||
} | ||
|
||
if !client.IsConnected { | ||
t.Error("Expected IsConnected to be true after Connect, but got false") | ||
} | ||
|
||
// Test Disconnect | ||
client.Disconnect() | ||
if client.IsConnected { | ||
t.Error("Expected IsConnected to be false after Disconnect, but got true") | ||
} | ||
} | ||
|
||
func TestConnectionFailed(t *testing.T) { | ||
ctx := context.Background() | ||
stationHash := "exampleHash" | ||
client := td2.New(ctx, stationHash) | ||
|
||
err := client.Connect("127.0.0.1:0") | ||
if err == nil { | ||
t.Error("Expected error on Connect, but got nothing") | ||
} | ||
} | ||
|
||
func TestWrite(t *testing.T) { | ||
ctx := context.Background() | ||
stationHash := "exampleHash" | ||
client := td2.New(ctx, stationHash) | ||
testData := "Hello, World!" | ||
expectedData := fmt.Sprintf("%s\r\n", testData) | ||
|
||
server := testserver.New() | ||
addr, err := server.Start() | ||
if err != nil { | ||
t.Fatalf("Failed to setup test server: %s", err) | ||
} | ||
|
||
err = client.Connect(addr) | ||
if err != nil { | ||
t.Errorf("Failed to connect: %s", err) | ||
} | ||
defer client.Disconnect() | ||
|
||
err = client.Write(testData) | ||
if err != nil { | ||
t.Errorf("Expected no error on Write operation, but got: %s", err) | ||
} | ||
|
||
receivedData := <-server.ReceivedDataChan | ||
if receivedData != expectedData { | ||
t.Errorf("Expected written data to be %q, but got %q", expectedData, receivedData) | ||
} | ||
} |