Skip to content

Commit

Permalink
cleanup test a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ainghazal committed Feb 1, 2024
1 parent a6cece1 commit 5a961eb
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions internal/reliabletransport/reliable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import (
"github.com/ooni/minivpn/internal/workers"
)

func initManagers() (*workers.Manager, *session.Manager) {
w := workers.NewManager(log.Log)
s, err := session.NewManager(log.Log)
if err != nil {
panic(err)
}
return w, s
}

// test that we're able to reorder (towards TLS) whatever is received (from the muxer).
func TestReliable_Reordering_withWorkers(t *testing.T) {

Expand All @@ -31,10 +40,10 @@ func TestReliable_Reordering_withWorkers(t *testing.T) {
name: "test proper ordering for input sequence",
args: args{
inputSequence: []string{
"[1] CONTROL_V1 +5ms",
"[2] CONTROL_V1 +5ms",
"[3] CONTROL_V1 +5ms",
"[4] CONTROL_V1 +5ms",
"[1] CONTROL_V1 +1ms",
"[2] CONTROL_V1 +1ms",
"[3] CONTROL_V1 +1ms",
"[4] CONTROL_V1 +1ms",
},
outputSequence: []int{1, 2, 3, 4},
},
Expand All @@ -43,10 +52,10 @@ func TestReliable_Reordering_withWorkers(t *testing.T) {
name: "test reordering for input sequence",
args: args{
inputSequence: []string{
"[2] CONTROL_V1 +5ms",
"[4] CONTROL_V1 +5ms",
"[3] CONTROL_V1 +5ms",
"[1] CONTROL_V1 +5ms",
"[2] CONTROL_V1 +1ms",
"[4] CONTROL_V1 +1ms",
"[3] CONTROL_V1 +1ms",
"[1] CONTROL_V1 +1ms",
},
outputSequence: []int{1, 2, 3, 4},
},
Expand All @@ -56,9 +65,9 @@ func TestReliable_Reordering_withWorkers(t *testing.T) {
args: args{
inputSequence: []string{
"[2] CONTROL_V1 +5ms",
"[4] CONTROL_V1 +50ms",
"[3] CONTROL_V1 +100ms",
"[1] CONTROL_V1 +100ms",
"[4] CONTROL_V1 +10ms",
"[3] CONTROL_V1 +1ms",
"[1] CONTROL_V1 +50ms",
},
outputSequence: []int{1, 2, 3, 4},
},
Expand All @@ -67,45 +76,41 @@ func TestReliable_Reordering_withWorkers(t *testing.T) {
name: "test reordering for input sequence, with duplicates",
args: args{
inputSequence: []string{
"[2] CONTROL_V1 +5ms",
"[2] CONTROL_V1 +5ms",
"[4] CONTROL_V1 +5ms",
"[4] CONTROL_V1 +5ms",
"[4] CONTROL_V1 +5ms",
"[1] CONTROL_V1 +5ms",
"[3] CONTROL_V1 +5ms",
"[1] CONTROL_V1 +5ms",
"[2] CONTROL_V1 +1ms",
"[2] CONTROL_V1 +1ms",
"[4] CONTROL_V1 +1ms",
"[4] CONTROL_V1 +1ms",
"[4] CONTROL_V1 +1ms",
"[1] CONTROL_V1 +1ms",
"[3] CONTROL_V1 +1ms",
"[1] CONTROL_V1 +1ms",
},
outputSequence: []int{1, 2, 3, 4},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

s := &Service{}

// just to properly initialize it, we don't care about these
s.ControlToReliable = make(chan *model.Packet)
dataToMuxer := make(chan *model.Packet)
s.DataOrControlToMuxer = &dataToMuxer

// the only two channels we're going to be testing on this test
dataIn := make(chan *model.Packet, 1024)
dataOut := make(chan *model.Packet, 1024)

workersManager := workers.NewManager(log.Log)
sessionManager, err := session.NewManager(log.Log)
if err != nil {
t.Errorf("Reordering: cannot create session.Manager: %v", err.Error())
}

s := &Service{
DataOrControlToMuxer: nil,
ControlToReliable: make(chan *model.Packet),
MuxerToReliable: dataIn,
ReliableToControl: nil,
}
s.DataOrControlToMuxer = &dataToMuxer
s.MuxerToReliable = dataIn
s.ReliableToControl = &dataOut
sessionID := sessionManager.LocalSessionID()

workers, session := initManagers()
sessionID := session.LocalSessionID()

// let the workers pump up the jam!
s.StartWorkers(log.Log, workersManager, sessionManager)
s.StartWorkers(log.Log, workers, session)

for _, testStr := range tt.args.inputSequence {
testPkt, err := vpntest.NewTestPacketFromString(testStr)
Expand All @@ -127,7 +132,6 @@ func TestReliable_Reordering_withWorkers(t *testing.T) {
wg.Add(1)
go func(ch <-chan *model.Packet) {
defer wg.Done()

got := make([]int, 0)
for {
// have we read enough packets to call it a day?
Expand All @@ -145,7 +149,6 @@ func TestReliable_Reordering_withWorkers(t *testing.T) {
t.Errorf("Reordering: got = %v, want %v", got, tt.args.outputSequence)
}
}(dataOut)

wg.Wait()
})
}
Expand Down

0 comments on commit 5a961eb

Please sign in to comment.