1// SPDX-License-Identifier: ISC
2// Copyright (c) 2014-2020 Bitmark Inc.
3// Use of this source code is governed by an ISC
4// license that can be found in the LICENSE file.
5
6package peer
7
8import (
9	"testing"
10
11	"github.com/golang/mock/gomock"
12	"github.com/stretchr/testify/assert"
13
14	"github.com/bitmark-inc/bitmarkd/peer/mocks"
15)
16
17func newTestConnector() *connector {
18	return &connector{}
19}
20
21func newTestMockUpstream(t *testing.T) (*gomock.Controller, *mocks.MockUpstream) {
22	ctl := gomock.NewController(t)
23	return ctl, mocks.NewMockUpstream(ctl)
24}
25
26func TestNextState(t *testing.T) {
27	c := newTestConnector()
28	orig := c.state
29	c.nextState(orig + 1)
30	assert.Equal(t, orig+1, c.state, "state not increased")
31}
32
33func TestGetConnectedClientCount(t *testing.T) {
34	c := newTestConnector()
35	ctl, mockUpstream := newTestMockUpstream(t)
36	defer ctl.Finish()
37
38	mockUpstream.EXPECT().IsConnected().Return(true).Times(1)
39	mockUpstream.EXPECT().IsConnected().Return(false).Times(1)
40
41	c.dynamicClients.PushBack(mockUpstream)
42	actual := c.getConnectedClientCount()
43	assert.Equal(t, 1, actual, "wrong connected client count")
44
45	actual = c.getConnectedClientCount()
46	assert.Equal(t, 0, actual, "wrong connected client count")
47}
48