Example #1
1
func TestAuth(t *testing.T) {
	opts := gnatsd.DefaultTestOptions
	opts.Port = 8232
	s := RunServerWithOptions(opts)

	// Auth is pluggable, so need to set here..
	auth := &auth.Plain{
		Username: "******",
		Password: "******",
	}
	s.SetAuthMethod(auth)

	defer s.Shutdown()

	_, err := nats.Connect("nats://*****:*****@localhost:8232")
	if err != nil {
		t.Fatal("Should have connected successfully")
	}
	nc.Close()
}
Example #2
1
func BenchmarkPublishSpeedViaChan(b *testing.B) {
	b.StopTimer()

	s := RunDefaultServer()
	defer s.Shutdown()

	nc, err := nats.Connect(nats.DefaultURL)
	if err != nil {
		b.Fatalf("Could not connect: %v\n", err)
	}
	ec, err := nats.NewEncodedConn(nc, nats.DEFAULT_ENCODER)
	defer ec.Close()

	ch := make(chan int32, 1024)
	if err := ec.BindSendChan("foo", ch); err != nil {
		b.Fatalf("Failed to bind to a send channel: %v\n", err)
	}

	b.StartTimer()

	num := int32(22)

	for i := 0; i < b.N; i++ {
		ch <- num
	}
	// Make sure they are all processed.
	nc.Flush()
	b.StopTimer()
}
Example #3
1
func NewConnection(t tLogger, port int) *nats.Conn {
	url := fmt.Sprintf("nats://localhost:%d", port)
	nc, err := nats.Connect(url)
	if err != nil {
		t.Fatalf("Failed to create default connection: %v\n", err)
		return nil
	}
	return nc
}
Example #4
1
func TestServerSecureConnections(t *testing.T) {
	t.Skip("Moved to gnatsd, waiting on TLS support")

	securePort := uint(2288)
	//	secureServer := startServer(t, securePort, "--ssl")
	//	defer secureServer.stopServer()
	secureUrl := fmt.Sprintf("nats://localhost:%d/", securePort)

	// Make sure this succeeds
	nc, err := nats.SecureConnect(secureUrl)
	if err != nil {
		t.Fatal("Failed to create secure (TLS) connection", err)
	}
	omsg := []byte("Hello World")
	received := 0
	nc.Subscribe("foo", func(m *nats.Msg) {
		received += 1
		if !bytes.Equal(m.Data, omsg) {
			t.Fatal("Message received does not match")
		}
	})
	err = nc.Publish("foo", omsg)
	if err != nil {
		t.Fatal("Failed to publish on secure (TLS) connection", err)
	}
	nc.Flush()
	nc.Close()

	// Test flag mismatch
	// Wanted but not available..
	nc, err = nats.SecureConnect(nats.DefaultURL)
	if err == nil || nc != nil || err != nats.ErrSecureConnWanted {
		t.Fatalf("Should have failed to create connection: %v", err)
	}

	// Server required, but not requested.
	nc, err = nats.Connect(secureUrl)
	if err == nil || nc != nil || err != nats.ErrSecureConnRequired {
		t.Fatal("Should have failed to create secure (TLS) connection")
	}
}
Example #5
1
func TestErrOnConnectAndDeadlock(t *testing.T) {
	// We will hand run a fake server that will timeout and not return a proper
	// INFO proto. This is to test that we do not deadlock. Issue #18

	l, e := net.Listen("tcp", ":0")
	if e != nil {
		t.Fatal("Could not listen on an ephemeral port")
	}
	tl := l.(*net.TCPListener)
	addr := tl.Addr().(*net.TCPAddr)

	go func() {
		conn, err := l.Accept()
		if err != nil {
			t.Fatalf("Error accepting client connection: %v\n", err)
		}
		defer conn.Close()
		// Send back a mal-formed INFO.
		conn.Write([]byte("INFOZ \r\n"))
	}()

	// Used to synchronize
	ch := make(chan bool)

	go func() {
		natsUrl := fmt.Sprintf("nats://localhost:%d/", addr.Port)
		_, err := nats.Connect(natsUrl)
		if err == nil {
			t.Fatal("Expected bad INFO err, got none")
		}
		ch <- true
	}()

	// Setup a timer to watch for deadlock
	select {
	case <-ch:
		break
	case <-time.After(time.Second):
		t.Fatalf("Connect took too long, deadlock?")
	}
}