Skip to content
Snippets Groups Projects
Commit ab20037f authored by Josh Brooks's avatar Josh Brooks
Browse files

Fix dummy message tests

parent e71ddf23
No related branches found
No related tags found
2 merge requests!510Release,!329Hotfix/dummy message bindings
......@@ -62,8 +62,21 @@ type Manager struct {
rng *fastRNG.StreamGenerator
}
// NewManager creates a new dummy Manager with the specified average send delta
// and the range used for generating random durations.
// NewManager creates a Manager object and initialises the
// dummy traffic sending thread. Note that the Manager does not start sending dummy
// traffic until True is passed into Manager.SetStatus. The time duration
// between each sending operation and the amount of messages sent each interval
// are randomly generated values with bounds defined by the
// given parameters below.
//
// Params:
// - maxNumMessages - the upper bound of the random number of messages sent
// each sending cycle.
// - avgSendDeltaMS - the average duration, in milliseconds, to wait
// between sends.
// - randomRangeMS - the upper bound of the interval between sending cycles.
// Sends occur every avgSendDeltaMS +/- a random duration with an
// upper bound of randomRangeMS
func NewManager(maxNumMessages int,
avgSendDelta, randomRange time.Duration,
net *xxdk.Cmix) *Manager {
......@@ -97,13 +110,19 @@ func (m *Manager) StartDummyTraffic() (stoppable.Stoppable, error) {
return stop, nil
}
// SetStatus sets the state of the dummy traffic send thread, which determines
// if the thread is running or paused. The possible statuses are:
// true = send thread is sending dummy messages
// false = send thread is paused/stopped and not sending dummy messages
// Returns an error if the channel is full.
// Note that this function cannot change the status of the send thread if it has
// yet to be started via StartDummyTraffic or if it has been stopped.
// SetStatus sets the state of the dummy traffic send thread by passing in
// a boolean parameter. There may be a small delay in between this call
// and the status of the sending thread to change accordingly. For example,
// passing False into this call while the sending thread is currently sending messages
// will not cancel nor halt the sending operation, but will pause the thread once that
// operation has completed.
//
// Params:
// - boolean - True: Sending thread is sending dummy messages.
// False: Sending thread is paused/stopped and is not sending dummy messages
// Returns:
// - error - if the DummyTraffic.SetStatus is called too frequently, causing the
// internal status channel to fill.
func (m *Manager) SetStatus(status bool) error {
select {
case m.statusChan <- status:
......@@ -113,13 +132,15 @@ func (m *Manager) SetStatus(status bool) error {
}
}
// GetStatus returns the current state of the dummy traffic send thread. It has
// the following return values:
// true = send thread is sending dummy messages
// false = send thread is paused/stopped and not sending dummy messages
// Note that this function does not return the status set by SetStatus directly;
// it returns the current status of the send thread, which means any call to
// SetStatus will have a small delay before it is returned by GetStatus.
// GetStatus returns the current state of the dummy traffic sending thread.
// Note that this function does not return the status set by the most recent call to
// SetStatus directly. Instead, this call returns the current status of the sending thread.
// This is due to the small delay that may occur between calling SetStatus and the
// sending thread taking into effect that status change.
//
// Returns:
// - boolean - True: Sending thread is sending dummy messages.
// - False: Sending thread is paused/stopped and is not sending dummy messages.
func (m *Manager) GetStatus() bool {
switch atomic.LoadUint32(&m.status) {
case running:
......
......@@ -27,7 +27,7 @@ func Test_newManager(t *testing.T) {
}
received := newManager(expected.maxNumMessages, expected.avgSendDelta,
expected.randomRange, nil, nil, nil, nil)
expected.randomRange, nil, nil, nil)
if statusChanLen != cap(received.statusChan) {
t.Errorf("Capacity of status channel unexpected."+
......@@ -118,10 +118,10 @@ func TestManager_SetStatus(t *testing.T) {
go func() {
var numReceived int
for i := 0; i < 2; i++ {
for m.networkManager.(*testNetworkManager).GetMsgListLen() == numReceived {
for m.net.(*mockCmix).GetMsgListLen() == numReceived {
time.Sleep(5 * time.Millisecond)
}
numReceived = m.networkManager.(*testNetworkManager).GetMsgListLen()
numReceived = m.net.(*mockCmix).GetMsgListLen()
msgChan <- true
}
}()
......@@ -161,7 +161,7 @@ func TestManager_SetStatus(t *testing.T) {
t.Errorf("Timed out after %s waiting for messages to be sent.",
3*m.avgSendDelta)
case <-msgChan:
numReceived += m.networkManager.(*testNetworkManager).GetMsgListLen()
numReceived += m.net.(*mockCmix).GetMsgListLen()
}
// Setting status to true multiple times does not interrupt sending
......@@ -177,10 +177,10 @@ func TestManager_SetStatus(t *testing.T) {
t.Errorf("Timed out after %s waiting for messages to be sent.",
3*m.avgSendDelta)
case <-msgChan:
if m.networkManager.(*testNetworkManager).GetMsgListLen() <= numReceived {
if m.net.(*mockCmix).GetMsgListLen() <= numReceived {
t.Errorf("Failed to receive second send."+
"\nmessages on last receive: %d\nmessages on this receive: %d",
numReceived, m.networkManager.(*testNetworkManager).GetMsgListLen())
numReceived, m.net.(*mockCmix).GetMsgListLen())
}
}
......@@ -254,10 +254,10 @@ func TestManager_GetStatus(t *testing.T) {
go func() {
var numReceived int
for i := 0; i < 2; i++ {
for m.networkManager.(*testNetworkManager).GetMsgListLen() == numReceived {
for m.net.(*mockCmix).GetMsgListLen() == numReceived {
time.Sleep(5 * time.Millisecond)
}
numReceived = m.networkManager.(*testNetworkManager).GetMsgListLen()
numReceived = m.net.(*mockCmix).GetMsgListLen()
msgChan <- true
}
}()
......@@ -292,7 +292,7 @@ func TestManager_GetStatus(t *testing.T) {
t.Errorf("Timed out after %s waiting for messages to be sent.",
3*m.avgSendDelta)
case <-msgChan:
numReceived += m.networkManager.(*testNetworkManager).GetMsgListLen()
numReceived += m.net.(*mockCmix).GetMsgListLen()
}
// Setting status to true multiple times does not interrupt sending
......@@ -311,10 +311,10 @@ func TestManager_GetStatus(t *testing.T) {
t.Errorf("Timed out after %s waiting for messages to be sent.",
3*m.avgSendDelta)
case <-msgChan:
if m.networkManager.(*testNetworkManager).GetMsgListLen() <= numReceived {
if m.net.(*mockCmix).GetMsgListLen() <= numReceived {
t.Errorf("Failed to receive second send."+
"\nmessages on last receive: %d\nmessages on this receive: %d",
numReceived, m.networkManager.(*testNetworkManager).GetMsgListLen())
numReceived, m.net.(*mockCmix).GetMsgListLen())
}
}
......
......@@ -39,7 +39,7 @@ func newMockCmix() cmix.Client {
func (m *mockCmix) Send(recipient *id.ID, fingerprint format.Fingerprint, service message.Service, payload, mac []byte, cmixParams cmix.CMIXParams) (id.Round, ephemeral.Id, error) {
m.Lock()
defer m.Unlock()
m.messages[*recipient] = payload
m.messages[*recipient] = fingerprint.Bytes()
return 0, ephemeral.Id{}, nil
}
......
......@@ -128,9 +128,10 @@ func TestManager_sendMessages(t *testing.T) {
receivedMsg, exists := receivedMsgs[recipient]
if !exists {
t.Errorf("Failed to receive message from %s: %+v", &recipient, msg)
} else if !reflect.DeepEqual(msg, receivedMsg) {
} else if !reflect.DeepEqual(msg.GetKeyFP().Bytes(), receivedMsg) {
// In mockCmix.Send, we map recipientId to the passed fingerprint.
t.Errorf("Received unexpected message for recipient %s."+
"\nexpected: %+v\nreceived: %+v", &recipient, msg, receivedMsg)
"\nexpected: %+v\nreceived: %+v", &recipient, msg.GetKeyFP(), receivedMsg)
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment