This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revamp ships to use new transport system
- Loading branch information
Showing
10 changed files
with
736 additions
and
314 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,151 @@ | ||
package ships | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/safing/spn/hub" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
testPort uint16 = 65000 | ||
testData = []byte("The quick brown fox jumps over the lazy dog") | ||
localhost = net.IPv4(127, 0, 0, 1) | ||
) | ||
|
||
func getTestPort() uint16 { | ||
testPort++ | ||
return testPort | ||
} | ||
|
||
func getTestBuf() []byte { | ||
return make([]byte, len(testData)) | ||
} | ||
|
||
func TestConnections(t *testing.T) { | ||
ctx := context.Background() | ||
var wg sync.WaitGroup | ||
|
||
registryLock.Lock() | ||
defer registryLock.Unlock() | ||
for protocol, builder := range registry { | ||
t.Run(protocol, func(t *testing.T) { | ||
|
||
// docking requests | ||
requests := make(chan *DockingRequest, 1) | ||
transport := &hub.Transport{ | ||
Protocol: protocol, | ||
Port: getTestPort(), | ||
} | ||
|
||
// create listener | ||
pier, err := builder.EstablishPier(ctx, transport, requests) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
wg.Add(1) | ||
go func() { //nolint:staticcheck // we wait for the goroutine | ||
err := pier.Docking(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
wg.Done() | ||
}() | ||
|
||
// connect to listener | ||
ship, err := builder.LaunchShip(ctx, transport, localhost) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// client send | ||
ok, err := ship.Load(testData) | ||
if err != nil { | ||
t.Fatalf("%s failed: %s", ship, err) | ||
} | ||
if !ok { | ||
t.Fatalf("%s sunk", ship) | ||
} | ||
|
||
// dock client | ||
request := <-requests | ||
if request.Err != nil { | ||
t.Fatalf("%s failed to dock: %s", request.Pier, request.Err) | ||
} | ||
srvShip := request.Ship | ||
|
||
// server recv | ||
buf := getTestBuf() | ||
_, ok, err = srvShip.UnloadTo(buf) | ||
if err != nil { | ||
t.Fatalf("%s failed: %s", ship, err) | ||
} | ||
if !ok { | ||
t.Fatalf("%s sunk", ship) | ||
} | ||
|
||
// check data | ||
assert.Equal(t, testData, buf, "should match") | ||
fmt.Print(".") | ||
|
||
for i := 0; i < 100; i++ { | ||
// server send | ||
ok, err = srvShip.Load(testData) | ||
if err != nil { | ||
t.Fatalf("%s failed: %s", ship, err) | ||
} | ||
if !ok { | ||
t.Fatalf("%s sunk", ship) | ||
} | ||
|
||
// client recv | ||
buf = getTestBuf() | ||
_, ok, err = ship.UnloadTo(buf) | ||
if err != nil { | ||
t.Fatalf("%s failed: %s", ship, err) | ||
} | ||
if !ok { | ||
t.Fatalf("%s sunk", ship) | ||
} | ||
|
||
// check data | ||
assert.Equal(t, testData, buf, "should match") | ||
fmt.Print(".") | ||
|
||
// client send | ||
ok, err = ship.Load(testData) | ||
if err != nil { | ||
t.Fatalf("%s failed: %s", ship, err) | ||
} | ||
if !ok { | ||
t.Fatalf("%s sunk", ship) | ||
} | ||
|
||
// server recv | ||
buf = getTestBuf() | ||
_, ok, err = srvShip.UnloadTo(buf) | ||
if err != nil { | ||
t.Fatalf("%s failed: %s", ship, err) | ||
} | ||
if !ok { | ||
t.Fatalf("%s sunk", ship) | ||
} | ||
|
||
// check data | ||
assert.Equal(t, testData, buf, "should match") | ||
fmt.Print(".") | ||
} | ||
|
||
ship.Sink() | ||
srvShip.Sink() | ||
pier.Abolish() | ||
wg.Wait() // wait for docking procedure to end | ||
|
||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.