fix test break

pull/82/head
v2ray 2016-02-04 13:27:35 +01:00
parent 3212337aa3
commit 4f59d6847f
2 changed files with 28 additions and 22 deletions

View File

@ -6,23 +6,29 @@ import (
) )
type TestPacketDispatcher struct { type TestPacketDispatcher struct {
LastPacket v2net.Packet LastPacket chan v2net.Packet
Handler func(packet v2net.Packet, traffic ray.OutboundRay) Handler func(packet v2net.Packet, traffic ray.OutboundRay)
} }
func (this *TestPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay { func NewTestPacketDispatcher(handler func(packet v2net.Packet, traffic ray.OutboundRay)) *TestPacketDispatcher {
traffic := ray.NewRay() if handler == nil {
this.LastPacket = packet handler = func(packet v2net.Packet, traffic ray.OutboundRay) {
if this.Handler == nil {
go func() {
for payload := range traffic.OutboundInput() { for payload := range traffic.OutboundInput() {
traffic.OutboundOutput() <- payload.Prepend([]byte("Processed: ")) traffic.OutboundOutput() <- payload.Prepend([]byte("Processed: "))
} }
close(traffic.OutboundOutput()) close(traffic.OutboundOutput())
}() }
} else {
go this.Handler(packet, traffic)
} }
return &TestPacketDispatcher{
LastPacket: make(chan v2net.Packet, 16),
Handler: handler,
}
}
func (this *TestPacketDispatcher) DispatchToOutbound(packet v2net.Packet) ray.InboundRay {
traffic := ray.NewRay()
this.LastPacket <- packet
go this.Handler(packet, traffic)
return traffic return traffic
} }

View File

@ -16,7 +16,7 @@ import (
func TestDokodemoTCP(t *testing.T) { func TestDokodemoTCP(t *testing.T) {
v2testing.Current(t) v2testing.Current(t)
testPacketDispatcher := &testdispatcher.TestPacketDispatcher{} testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
data2Send := "Data to be sent to remote." data2Send := "Data to be sent to remote."
@ -42,21 +42,23 @@ func TestDokodemoTCP(t *testing.T) {
tcpClient.Write([]byte(data2Send)) tcpClient.Write([]byte(data2Send))
tcpClient.CloseWrite() tcpClient.CloseWrite()
lastPacket := <-testPacketDispatcher.LastPacket
response := make([]byte, 1024) response := make([]byte, 1024)
nBytes, err := tcpClient.Read(response) nBytes, err := tcpClient.Read(response)
assert.Error(err).IsNil() assert.Error(err).IsNil()
tcpClient.Close() tcpClient.Close()
assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes])) assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
assert.Bool(testPacketDispatcher.LastPacket.Destination().IsTCP()).IsTrue() assert.Bool(lastPacket.Destination().IsTCP()).IsTrue()
netassert.Address(testPacketDispatcher.LastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{1, 2, 3, 4})) netassert.Address(lastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{1, 2, 3, 4}))
netassert.Port(testPacketDispatcher.LastPacket.Destination().Port()).Equals(128) netassert.Port(lastPacket.Destination().Port()).Equals(128)
} }
func TestDokodemoUDP(t *testing.T) { func TestDokodemoUDP(t *testing.T) {
v2testing.Current(t) v2testing.Current(t)
testPacketDispatcher := &testdispatcher.TestPacketDispatcher{} testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
data2Send := "Data to be sent to remote." data2Send := "Data to be sent to remote."
@ -80,14 +82,12 @@ func TestDokodemoUDP(t *testing.T) {
assert.Error(err).IsNil() assert.Error(err).IsNil()
udpClient.Write([]byte(data2Send)) udpClient.Write([]byte(data2Send))
response := make([]byte, 1024)
nBytes, err := udpClient.Read(response)
assert.Error(err).IsNil()
udpClient.Close() udpClient.Close()
assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes])) lastPacket := <-testPacketDispatcher.LastPacket
assert.Bool(testPacketDispatcher.LastPacket.Destination().IsUDP()).IsTrue()
netassert.Address(testPacketDispatcher.LastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{5, 6, 7, 8})) assert.StringLiteral(data2Send).Equals(string(lastPacket.Chunk().Value))
netassert.Port(testPacketDispatcher.LastPacket.Destination().Port()).Equals(256) assert.Bool(lastPacket.Destination().IsUDP()).IsTrue()
netassert.Address(lastPacket.Destination().Address()).Equals(v2net.IPAddress([]byte{5, 6, 7, 8}))
netassert.Port(lastPacket.Destination().Port()).Equals(256)
} }