forked from openprivacy/libricochet-go
Testing ContactChannel, Fixing CloseChannel Conditions
This commit is contained in:
parent
a04b3fe08b
commit
f9209e187c
|
@ -131,20 +131,22 @@ func (crc *ContactRequestChannel) OpenOutboundResult(err error, crm *Protocol_Da
|
|||
func (crc *ContactRequestChannel) SendResponse(status string) {
|
||||
messageBuilder := new(utils.MessageBuilder)
|
||||
crc.channel.SendMessage(messageBuilder.ReplyToContactRequest(crc.channel.ID, status))
|
||||
crc.channel.CloseChannel()
|
||||
}
|
||||
|
||||
func (crc *ContactRequestChannel) handleStatus(status string) {
|
||||
switch status {
|
||||
case "Accepted":
|
||||
crc.Handler.ContactRequestAccepted()
|
||||
crc.channel.CloseChannel()
|
||||
case "Pending":
|
||||
break
|
||||
case "Rejected":
|
||||
crc.Handler.ContactRequestRejected()
|
||||
break
|
||||
crc.channel.CloseChannel()
|
||||
case "Error":
|
||||
crc.Handler.ContactRequestError()
|
||||
break
|
||||
crc.channel.CloseChannel()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -157,6 +159,4 @@ func (crc *ContactRequestChannel) Packet(data []byte) {
|
|||
crc.handleStatus(response.GetStatus().String())
|
||||
}
|
||||
}
|
||||
// Whatever happens we close the channel
|
||||
crc.channel.CloseChannel()
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@ func TestContactRequestOpenOutbound(t *testing.T) {
|
|||
handler := new(TestContactRequestHandler)
|
||||
contactRequestChannel.Handler = handler
|
||||
channel := Channel{ID: 1}
|
||||
channel.CloseChannel = func() {}
|
||||
response, err := contactRequestChannel.OpenOutbound(&channel)
|
||||
if err == nil {
|
||||
res := new(Protocol_Data_Control.Packet)
|
||||
|
@ -73,6 +74,7 @@ func TestContactRequestOpenOutboundResult(t *testing.T) {
|
|||
Handler: &TestContactRequestHandler{},
|
||||
}
|
||||
channel := Channel{ID: 1}
|
||||
channel.CloseChannel = func() {}
|
||||
contactRequestChannel.OpenOutbound(&channel)
|
||||
|
||||
messageBuilder := new(utils.MessageBuilder)
|
||||
|
@ -93,6 +95,7 @@ func TestContactRequestOpenInbound(t *testing.T) {
|
|||
handler := new(TestContactRequestHandler)
|
||||
contactRequestChannel.Handler = handler
|
||||
channel := Channel{ID: 1}
|
||||
channel.CloseChannel = func() {}
|
||||
response, err := contactRequestChannel.OpenInbound(&channel, opm)
|
||||
|
||||
if err == nil {
|
||||
|
@ -147,6 +150,46 @@ func TestContactRequestPacket(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContactRequestPending(t *testing.T) {
|
||||
contactRequestChannel := new(ContactRequestChannel)
|
||||
handler := new(TestContactRequestHandler)
|
||||
contactRequestChannel.Handler = handler
|
||||
channel := Channel{ID: 1}
|
||||
contactRequestChannel.OpenOutbound(&channel)
|
||||
|
||||
messageBuilder := new(utils.MessageBuilder)
|
||||
ack := messageBuilder.ReplyToContactRequestOnResponse(1, "Pending")
|
||||
// We have just constructed this so there is little
|
||||
// point in doing error checking here in the test
|
||||
res := new(Protocol_Data_Control.Packet)
|
||||
proto.Unmarshal(ack[:], res)
|
||||
cr := res.GetChannelResult()
|
||||
|
||||
contactRequestChannel.OpenOutboundResult(nil, cr)
|
||||
|
||||
ackp := messageBuilder.ReplyToContactRequest(1, "Pending")
|
||||
closed := false
|
||||
channel.CloseChannel = func() { closed = true }
|
||||
contactRequestChannel.Packet(ackp)
|
||||
if closed {
|
||||
t.Errorf("Channel Should Not Have Been Closed")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestContactRequestSend(t *testing.T) {
|
||||
contactRequestChannel := new(ContactRequestChannel)
|
||||
channel := Channel{ID: 1}
|
||||
channel.SendMessage = func(message []byte) {}
|
||||
closed := false
|
||||
channel.CloseChannel = func() { closed = true }
|
||||
contactRequestChannel.OpenOutbound(&channel)
|
||||
contactRequestChannel.SendResponse("Accepted")
|
||||
if closed != true {
|
||||
t.Errorf("Channel Should Not Have Been Closed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestContactRequestRejected(t *testing.T) {
|
||||
contactRequestChannel := new(ContactRequestChannel)
|
||||
handler := new(TestContactRequestHandler)
|
||||
|
|
Loading…
Reference in New Issue