Exemplo n.º 1
0
func TestSimpleEndpoint(t *testing.T) {
	t.Parallel()
	logs.ResetLogger()

	if testing.Short() {
		t.Skip("this is a long running test.")
	}

	assert := assert.New(t)

	ka, err := cipherset.GenerateKey(0x3a)
	assert.NoError(err)

	kb, err := cipherset.GenerateKey(0x3a)
	assert.NoError(err)

	ea, erra := Open(
		Keys(cipherset.Keys{0x3a: ka}),
		Transport(mux.Config{udp.Config{}, inproc.Config{}}),
		Log(nil))

	eb, errb := Open(
		Keys(cipherset.Keys{0x3a: kb}),
		Transport(mux.Config{udp.Config{}, inproc.Config{}}),
		Log(nil))
	assert.NoError(erra)
	assert.NoError(errb)

	time.Sleep(1 * time.Second)

	identA, err := ea.LocalIdentity()
	assert.NoError(err)

	identB, err := eb.LocalIdentity()
	assert.NoError(err)

	_, err = ea.Dial(identB)
	assert.NoError(err)

	_, err = ea.Dial(identB)
	assert.NoError(err)

	_, err = eb.Dial(identA)
	assert.NoError(err)

	time.Sleep(2*time.Minute + 10*time.Second)

	err = ea.Close()
	assert.NoError(err)

	err = eb.Close()
	assert.NoError(err)
}
Exemplo n.º 2
0
func TestPingPong(t *testing.T) {
	// t.Parallel()
	logs.ResetLogger()

	withTwoEndpoints(t, func(A, B *Endpoint) {
		var (
			assert = assert.New(t)
			c      *Channel
			ident  *Identity
			pkt    *lob.Packet
			err    error
		)

		go func() {
			c, err := A.Listen("ping", false).AcceptChannel()

			c.SetDeadline(time.Now().Add(10 * time.Second))

			if assert.NoError(err) && assert.NotNil(c) {
				defer c.Close()

				pkt, err = c.ReadPacket()
				if assert.NoError(err) && assert.NotNil(pkt) {
					assert.Equal("ping", string(pkt.Body(nil)))

					err = c.WritePacket(lob.New([]byte("pong")))
					assert.NoError(err)
				}
			}
		}()

		ident, err = A.LocalIdentity()
		assert.NoError(err)

		c, err = B.Open(ident, "ping", false)
		assert.NoError(err)
		if assert.NotNil(c) {
			defer c.Close()

			c.SetDeadline(time.Now().Add(10 * time.Second))

			err = c.WritePacket(lob.New([]byte("ping")))
			assert.NoError(err)

			pkt, err = c.ReadPacket()
			if assert.NoError(err) && assert.NotNil(pkt) {
				assert.Equal("pong", string(pkt.Body(nil)))
			}
		}
	})
}
Exemplo n.º 3
0
func BenchmarkChannelsReliable(b *testing.B) {
	defer dumpExpVar(b)
	logs.ResetLogger()

	var (
		ping = []byte("ping")
		pong = []byte("pong")
	)

	client := func(x *Exchange) {
		c, err := x.Open("ping", true)
		if err != nil {
			b.Fatal(err)
		}

		defer c.Close()

		pkt := lob.New(ping)
		err = c.WritePacket(pkt)
		if err != nil {
			b.Fatal(err)
		}

		pkt, err = c.ReadPacket()
		if err != nil {
			b.Fatal(err)
		}
		pkt.Free()
	}

	server := func(c *Channel) {
		defer c.Close()

		pkt, err := c.ReadPacket()
		if err != nil {
			b.Fatal(err)
		}
		pkt.Free()

		pkt = lob.New(pong)
		err = c.WritePacket(pkt)
		if err != nil {
			b.Fatal(err)
		}
	}

	accept := func(l *Listener) {
		for {
			c, err := l.AcceptChannel()
			if err == io.EOF {
				break
			}
			if err != nil {
				b.Fatal(err)
			}
			go server(c)
		}
	}

	withTwoEndpoints(b, func(A, B *Endpoint) {
		A.setOptions(DisableLog())
		B.setOptions(DisableLog())

		var (
			ident *Identity
			err   error
		)

		b.ResetTimer()

		l := A.Listen("ping", true)
		defer l.Close()
		go accept(l)

		ident, err = A.LocalIdentity()
		if err != nil {
			b.Fatal(err)
		}

		x, err := B.Dial(ident)
		if err != nil {
			b.Fatal(err)
		}

		b.ResetTimer()

		for i := 0; i < b.N; i++ {
			client(x)
		}

		b.StopTimer()
	})
}
Exemplo n.º 4
0
func BenchmarkReadWriteUnreliable(b *testing.B) {
	defer dumpExpVar(b)
	logs.ResetLogger()

	withTwoEndpoints(b, func(A, B *Endpoint) {
		A.setOptions(DisableLog())
		B.setOptions(DisableLog())

		var (
			c     *Channel
			ident *Identity
			pkt   *lob.Packet
			err   error
			body  = bytes.Repeat([]byte{'x'}, 1300)
		)

		b.SetBytes(int64(len(body)))
		b.ResetTimer()

		go func() {
			c, err := A.Listen("flood", false).AcceptChannel()
			if err != nil {
				b.Fatal(err)
			}

			defer c.Close()

			pkt, err = c.ReadPacket()
			if err != nil {
				b.Fatal(err)
			}

			for i := 0; i < b.N; i++ {
				pkt := lob.New(body)
				err = c.WritePacket(pkt)
				if err != nil {
					b.Fatal(err)
				}

				// Give the other go routines some room to breath when GOMAXPROCS=1
				runtime.Gosched()
			}
		}()

		ident, err = A.LocalIdentity()
		if err != nil {
			b.Fatal(err)
		}

		c, err = B.Open(ident, "flood", false)
		if err != nil {
			b.Fatal(err)
		}

		defer c.Close()

		err = c.WritePacket(lob.New(nil))
		if err != nil {
			b.Fatal(err)
		}

		for {
			pkt, err = c.ReadPacket()
			if err == io.EOF {
				break
			}
			if err != nil {
				b.Fatal(err)
			}
			pkt.Free()
		}

		b.StopTimer()
	})
}