Esempio n. 1
0
func TestMockHyperbahn(t *testing.T) {
	mh, err := mockhyperbahn.New()
	require.NoError(t, err, "mock hyperbahn failed")
	defer mh.Close()

	config.hyperbahnConfig = mh.Configuration()
	require.NoError(t, setupServer(), "setupServer failed")
	assert.Equal(t, []string{"myservice"}, mh.GetAdvertised())
}
Esempio n. 2
0
func withSetup(t *testing.T, f func(mh *mockhyperbahn.Mock, client *Client)) {
	mh, err := mockhyperbahn.New()
	require.NoError(t, err, "mockhyperbahn.New failed")
	defer mh.Close()

	clientCh := testutils.NewClient(t, nil)
	client, err := NewClient(clientCh, mh.Configuration(), nil)
	require.NoError(t, err, "NewClient failed")
	defer clientCh.Close()

	f(mh, client)
}
Esempio n. 3
0
func TestMockIgnoresDown(t *testing.T) {
	mockHB, err := mockhyperbahn.New()
	require.NoError(t, err, "Failed to set up mock hyperbahm")

	var (
		moe1Called atomic.Bool
		moe2Called atomic.Bool
	)

	moe1 := newAdvertisedEchoServer(t, "moe", mockHB, func() { moe1Called.Store(true) })
	defer moe1.Close()
	moe2 := newAdvertisedEchoServer(t, "moe", mockHB, func() { moe2Called.Store(true) })
	defer moe2.Close()
	client := newAdvertisedEchoServer(t, "client", mockHB, nil)

	ctx, cancel := tchannel.NewContext(time.Second)
	defer cancel()

	for i := 0; i < 20; i++ {
		_, _, _, err = raw.CallSC(ctx, client.GetSubChannel("moe"), "echo", nil, nil)
		assert.NoError(t, err, "Call failed")
	}

	require.True(t, moe1Called.Load(), "moe1 not called")
	require.True(t, moe2Called.Load(), "moe2 not called")

	// If moe2 is brought down, all calls should now be sent to moe1.
	moe2.Close()

	// Wait for the mock HB to have 0 connections to moe
	ok := testutils.WaitFor(time.Second, func() bool {
		in, out := mockHB.Channel().Peers().GetOrAdd(moe2.PeerInfo().HostPort).NumConnections()
		return in+out == 0
	})
	require.True(t, ok, "Failed waiting for mock HB to have 0 connections")

	// Make sure that all calls succeed (they should all go to moe2)
	moe1Called.Store(false)
	moe2Called.Store(false)
	for i := 0; i < 20; i++ {
		_, _, _, err = raw.CallSC(ctx, client.GetSubChannel("moe"), "echo", nil, nil)
		assert.NoError(t, err, "Call failed")
	}

	require.True(t, moe1Called.Load(), "moe1 not called")
	require.False(t, moe2Called.Load(), "moe2 should not be called after Close")
}
Esempio n. 4
0
func TestMockDiscovery(t *testing.T) {
	mh, err := mockhyperbahn.New()
	require.NoError(t, err, "mock hyperbahn failed")
	defer mh.Close()

	peers := []string{
		"1.3.5.7:1456",
		"255.255.255.255:25",
	}
	mh.SetDiscoverResult("discover-svc", peers)

	config.hyperbahnConfig = mh.Configuration()
	client, err := setupServer()
	require.NoError(t, err, "setupServer failed")

	gotPeers, err := client.Discover("discover-svc")
	require.NoError(t, err, "Discover failed")
	assert.Equal(t, peers, gotPeers, "Discover returned invalid peers")
}
Esempio n. 5
0
func TestMockForwards(t *testing.T) {
	mockHB, err := mockhyperbahn.New()
	require.NoError(t, err, "Failed to set up mock hyperbahm")

	called := false
	server := newAdvertisedEchoServer(t, "svr", mockHB, func() {
		called = true
	})
	defer server.Close()
	client := newAdvertisedEchoServer(t, "client", mockHB, nil)
	defer client.Close()

	ctx, cancel := tchannel.NewContext(time.Second)
	defer cancel()

	_, _, _, err = raw.CallSC(ctx, client.GetSubChannel("svr"), "echo", nil, nil)
	require.NoError(t, err, "Call failed")
	require.True(t, called, "Advertised server was not called")
}