2023-08-17 10:56:08 -03:00
|
|
|
import unittest
|
|
|
|
|
|
2023-08-18 16:09:04 -07:00
|
|
|
import swarmsim/engine/eventdrivenengine
|
|
|
|
|
import swarmsim/engine/network
|
|
|
|
|
import swarmsim/engine/peer
|
|
|
|
|
import swarmsim/engine/protocol
|
2023-08-17 10:56:08 -03:00
|
|
|
|
|
|
|
|
type
|
|
|
|
|
FakeProtocol = ref object of Protocol
|
|
|
|
|
received: bool
|
|
|
|
|
|
2023-08-17 19:16:03 -03:00
|
|
|
method uncheckedDeliver(self: FakeProtocol, message: Message,
|
|
|
|
|
engine: EventDrivenEngine, network: Network) =
|
2023-08-17 10:56:08 -03:00
|
|
|
self.received = true
|
|
|
|
|
|
|
|
|
|
proc getFakeProtocol(peer: Peer, protocolId: string): FakeProtocol =
|
|
|
|
|
let protocol = peer.getProtocol(protocolId)
|
|
|
|
|
check(protocol.isSome)
|
|
|
|
|
return FakeProtocol(protocol.get())
|
|
|
|
|
|
|
|
|
|
suite "network":
|
|
|
|
|
test "should dispatch message to the correct protocol within a peer":
|
|
|
|
|
let engine = EventDrivenEngine()
|
|
|
|
|
|
2023-08-17 16:50:41 -03:00
|
|
|
let peer = Peer.new(
|
|
|
|
|
protocols = @[
|
|
|
|
|
Protocol FakeProtocol(messageType: "protocol1", received: false),
|
|
|
|
|
FakeProtocol(messageType: "protocol2", received: false)
|
|
|
|
|
]
|
|
|
|
|
)
|
2023-08-17 10:56:08 -03:00
|
|
|
let network = Network.new(engine = engine, defaultLinkDelay = 20)
|
|
|
|
|
|
|
|
|
|
network.add(peer)
|
|
|
|
|
|
2023-08-18 19:09:02 -03:00
|
|
|
let m1 = Message(receiver: peer, messageType: "protocol1")
|
|
|
|
|
let m2 = Message(receiver: peer, messageType: "protocol2")
|
2023-08-17 10:56:08 -03:00
|
|
|
|
|
|
|
|
let message2handle = network.send(m2, linkDelay = uint64(10).some)
|
|
|
|
|
let message1handle = network.send(m1, linkDelay = uint64(5).some)
|
|
|
|
|
|
|
|
|
|
check(not peer.getFakeProtocol("protocol1").received)
|
|
|
|
|
check(not peer.getFakeProtocol("protocol2").received)
|
|
|
|
|
|
|
|
|
|
message1Handle.doAwait()
|
|
|
|
|
|
|
|
|
|
check(peer.getFakeProtocol("protocol1").received)
|
|
|
|
|
check(not peer.getFakeProtocol("protocol2").received)
|
|
|
|
|
|
|
|
|
|
message2Handle.doAwait()
|
|
|
|
|
|
|
|
|
|
check(peer.getFakeProtocol("protocol1").received)
|
|
|
|
|
check(peer.getFakeProtocol("protocol2").received)
|
|
|
|
|
|