mock circuit generator can now create multiple circuits

This commit is contained in:
David Vennik
2023-01-16 17:43:29 +00:00
parent c7650ba9ff
commit 17cf757192
2 changed files with 48 additions and 17 deletions

View File

@@ -21,7 +21,7 @@ func TestPing(t *testing.T) {
const nTotal = 6
clients := make([]*Client, nTotal)
var e error
if clients, e = CreateMockCircuitClients(true); check(e) {
if clients, e = CreateNMockCircuits(true, 1); check(e) {
t.Error(e)
t.FailNow()
}
@@ -62,7 +62,7 @@ func TestSendExit(t *testing.T) {
const nTotal = 6
clients := make([]*Client, nTotal)
var e error
if clients, e = CreateMockCircuitClients(true); check(e) {
if clients, e = CreateNMockCircuits(true, 1); check(e) {
t.Error(e)
t.FailNow()
}
@@ -107,9 +107,12 @@ func TestSendExit(t *testing.T) {
log.T.Ln(clients[0].Node.AddrPort.String())
clients[0].Node.Send(b)
go func() {
time.Sleep(time.Second * 6)
quit.Q()
t.Error("SendExit got stuck")
select {
case <-time.After(time.Second):
t.Error("sendexit got stuck")
quit.Q()
case <-quit:
}
}()
bb := <-clients[3].Services[0].Receive()
log.T.S(bb.ToBytes())
@@ -126,7 +129,7 @@ func TestSendExit(t *testing.T) {
func TestSendKeys(t *testing.T) {
var clients []*Client
var e error
if clients, e = CreateMockCircuitClients(false); check(e) {
if clients, e = CreateNMockCircuits(false, 1); check(e) {
t.Error(e)
t.FailNow()
}
@@ -136,9 +139,12 @@ func TestSendKeys(t *testing.T) {
}
quit := qu.T()
go func() {
<-time.After(time.Second * 2)
quit.Q()
t.Error("SendKeys got stuck")
select {
case <-time.After(time.Second):
t.Error("sendkeys got stuck")
quit.Q()
case <-quit:
}
}()
cnf := nonce.NewID()
var sess [5]*session.OnionSkin
@@ -177,5 +183,30 @@ func TestSendKeys(t *testing.T) {
for _, v := range clients {
v.Shutdown()
}
}
func TestGetBalance(t *testing.T) {
var clients []*Client
var e error
if clients, e = CreateNMockCircuits(false, 2); check(e) {
t.Error(e)
t.FailNow()
}
// Start up the clients.
for _, v := range clients {
go v.Start()
}
quit := qu.T()
go func() {
select {
case <-time.After(time.Second):
// t.Error("ping got stuck")
quit.Q()
case <-quit:
}
}()
<-quit.Wait()
for _, v := range clients {
v.Shutdown()
}
}

View File

@@ -12,11 +12,10 @@ import (
"github.com/indra-labs/indra/pkg/transport"
)
const nTotal = 6
func CreateMockCircuitClients(inclSessions bool) (cl []*Client,
e error) {
func CreateNMockCircuits(inclSessions bool,
nCircuits int) (cl []*Client, e error) {
nTotal := 1 + nCircuits*5
cl = make([]*Client, nTotal)
nodes := make([]*node.Node, nTotal)
transports := make([]ifc.Transport, nTotal)
@@ -41,7 +40,10 @@ func CreateMockCircuitClients(inclSessions bool) (cl []*Client,
if inclSessions {
// create a session for all but the first
if i > 0 {
sessions[i-1] = node.NewSession(nonce.NewID(), nodes[i], math.MaxUint64, nil, nil, byte(i-1))
sessions[i-1] = node.NewSession(
nonce.NewID(), nodes[i],
math.MaxUint64, nil, nil,
byte(i/nCircuits-1))
// Add session to node, so it will be able to
// relay if it gets a message with the key.
nodes[i].AddSession(sessions[i-1])
@@ -49,8 +51,6 @@ func CreateMockCircuitClients(inclSessions bool) (cl []*Client,
}
}
}
// Add each node to each other's Nodes except itself, this enables them
// to send messages across their transports to each other.
for i := range cl {
for j := range nodes {
if i == j {