trac-msb 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,7 +50,6 @@ test('ConnectionManager', () => {
50
50
  reset()
51
51
  const connectionManager = makeManager()
52
52
  t.is(connectionManager.connectionCount(), connections.length, 'should have the same length')
53
-
54
53
  const data = createConnection(testKeyPair5.publicKey)
55
54
  connectionManager.addValidator(data.key, data.connection)
56
55
  t.is(connectionManager.connectionCount(), connections.length + 1, 'should have the same length')
@@ -70,6 +69,31 @@ test('ConnectionManager', () => {
70
69
  connectionManager.addValidator(toNotAdd.key, toNotAdd.connection)
71
70
  t.is(connectionManager.connectionCount(), maxConnections, 'should not increase length')
72
71
  })
72
+
73
+ test('does not add new validator when pool is full', async t => {
74
+ reset()
75
+ const maxConnections = 2
76
+ const localConnections = [
77
+ createConnection(testKeyPair1.publicKey),
78
+ createConnection(testKeyPair2.publicKey),
79
+ ]
80
+
81
+ const connectionManager = new ConnectionManager({ maxValidators: maxConnections })
82
+ localConnections.forEach(({ key, connection }) => {
83
+ connectionManager.addValidator(key, connection)
84
+ })
85
+
86
+ t.is(connectionManager.connectionCount(), maxConnections, 'pool should be full')
87
+
88
+ const newConn = createConnection(testKeyPair3.publicKey)
89
+ connectionManager.addValidator(newConn.key, newConn.connection)
90
+
91
+ t.is(connectionManager.connectionCount(), maxConnections, 'should stay at max size')
92
+ t.not(connectionManager.connected(newConn.key), 'new validator should not be in the pool')
93
+
94
+ const remainingOld = localConnections.filter(c => connectionManager.connected(c.key)).length
95
+ t.is(remainingOld, 2, 'all of the old validators should remain')
96
+ })
73
97
  })
74
98
 
75
99
  test('connected', async t => {
@@ -93,84 +117,29 @@ test('ConnectionManager', () => {
93
117
  reset()
94
118
  const connectionManager = makeManager()
95
119
 
96
- connectionManager.send([1,2,3,4])
97
- t.ok(connections[0].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
98
- })
120
+ const target = connectionManager.send([1,2,3,4])
99
121
 
100
- test('rotate the messenger', async t => {
101
- reset()
102
- const connectionManager = makeManager()
103
-
104
- for (let i = 0; i < 10; i++) {
105
- connectionManager.send([1,2,3,4])
106
- t.ok(connections[0].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
107
- }
122
+ const totalCalls = connections.reduce((sum, con) => sum + con.connection.messenger.send.callCount, 0)
123
+ t.is(totalCalls, 1, 'should send to exactly one validator')
124
+ t.ok(target, 'should return a target public key')
108
125
  })
109
126
 
110
- test('resets rotation', async t => {
127
+ test('does not throw on individual send errors', async t => {
111
128
  reset()
112
- const connectionManager = makeManager()
113
-
114
- for (let i = 0; i < 10; i++) {
115
- connectionManager.send([1,2,3,4])
116
- t.ok(connections[0].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
117
- }
118
-
119
- for (let i = 0; i < 10; i++) {
120
- connectionManager.send([1,2,3,4])
121
- t.ok(connections[1].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
122
- }
123
-
124
- for (let i = 0; i < 10; i++) {
125
- connectionManager.send([1,2,3,4])
126
- t.ok(connections[2].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
127
- }
128
-
129
- for (let i = 0; i < 10; i++) {
130
- connectionManager.send([1,2,3,4])
131
- t.ok(connections[3].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
132
- }
133
-
134
- t.is(connections[0].connection.messenger.send.callCount, 10, 'first should have been called 10 times')
135
- t.is(connections[1].connection.messenger.send.callCount, 10, 'second should have been called 10 times')
136
- t.is(connections[2].connection.messenger.send.callCount, 10, 'third should have been called 10 times')
137
- t.is(connections[3].connection.messenger.send.callCount, 10, 'fourth should have been called 10 times')
138
-
139
- connectionManager.send([1,2,3,4])
140
- t.ok(connections[0].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
141
- t.is(connections[0].connection.messenger.send.callCount, 11, 'first should have been called 11 times')
142
- })
143
-
144
- test('rotates on exception', async t => {
145
- reset()
146
- const conns = [
129
+ const errorConnections = [
147
130
  createConnection(testKeyPair7.publicKey),
148
131
  createConnection(testKeyPair8.publicKey),
149
- createConnection(testKeyPair9.publicKey),
150
132
  ]
151
133
 
152
- conns[0].connection.messenger.send = sinon.stub().throws(new Error())
153
- conns[1].connection.messenger.send = sinon.stub().throws(new Error())
154
-
155
- const connectionManager = makeManager(5, conns)
156
- connectionManager.send([1,2,3,4])
157
-
158
- t.is(conns[0].connection.messenger.send.callCount, 1, 'first should have been called 10 times')
159
- t.is(conns[1].connection.messenger.send.callCount, 1, 'second should have been called 10 times')
160
- t.is(conns[2].connection.messenger.send.callCount, 1, 'third should have been called 10 times')
161
- })
162
- })
134
+ errorConnections.forEach(con => {
135
+ con.connection.messenger.send = sinon.stub().throws(new Error())
136
+ })
163
137
 
164
- test('rotate', async t => {
165
- test('resets the rotation', async t => {
166
- reset()
167
- const connectionManager = makeManager()
138
+ const connectionManager = makeManager(5, errorConnections)
168
139
 
169
- connectionManager.send([1,2,3,4]) // this shouldnt trigger rotation
170
- t.ok(connections[0].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
171
- connectionManager.rotate() // rotate
140
+ t.is(errorConnections.length, 2, 'should have two connections')
172
141
  connectionManager.send([1,2,3,4])
173
- t.ok(connections[1].connection.messenger.send.calledWith([1,2,3,4]), 'first on the list should have been called')
142
+ t.ok(true, 'send should not throw even if individual sends fail')
174
143
  })
175
144
  })
176
145
 
@@ -216,4 +185,4 @@ test('ConnectionManager', () => {
216
185
  t.is(connectionCount, connectionManager.connectionCount() + 1, 'first on the list should have been called')
217
186
  })
218
187
  })
219
- })
188
+ })