Skip to content

Commit

Permalink
test: peer fetch coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivelin Ivanov authored Jun 17, 2021
2 parents 1bb7ec7 + 66cdb5b commit 3b32a32
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 104 deletions.
208 changes: 105 additions & 103 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions tests/unit/remote/peer-fetch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,70 @@ test('PeerFetch _stopPing()', async () => {
expect(clearInterval).toHaveBeenCalledTimes(1)
expect(clearInterval).toHaveBeenCalledWith(timer)
})

test('PeerFetch _dataConnection.on("data", ...)', async () => {
const dataConnection = jest.fn()
dataConnection.on = jest.fn()
const peerFetch = new PeerFetch(dataConnection)
// get the callback function registered for dataConnection.on('data')
const onDataCallback = peerFetch._dataConnection.on.mock.calls.find(callbackDetails => callbackDetails[0] === 'data')[1]
// first test the case when data arrives without a matching get request
const cerror = jest.spyOn(console, 'error').mockImplementation(() => {})
onDataCallback('some http response data')
expect(cerror).toHaveBeenCalledTimes(1)
cerror.mockReset()
// next test the case when data arrives in response to a get request
const request = {
url: '/',
method: 'GET'
}
const ticket = peerFetch._enqueueRequest(request)
// test sub-use case: initial response packet has no status attribute
const cwarn = jest.spyOn(console, 'warn').mockImplementation(() => {})
onDataCallback('{}')
expect(cwarn).toHaveBeenCalledWith('Remote peer may not be using a compatible protocol.')
cwarn.mockReset()
// test sub-use case: initial response packet arrives with header info
const cdebug1 = jest.spyOn(console, 'debug').mockImplementation(() => {})
onDataCallback('{"status": 202}')
expect(cdebug1).toHaveBeenCalledWith('Received keepalive ping')
cdebug1.mockReset()
// test sub-use case: final status response packet arrives
const cdebug2 = jest.spyOn(console, 'debug').mockImplementation(() => {})
onDataCallback('{"status": 200}')
expect(cdebug2).toHaveBeenCalledWith('Received web server final response header', expect.anything())
cdebug2.mockReset()
let pair = peerFetch._requestMap.get(ticket)
expect(pair.response.receivedAll).toBeFalse()
// test sub-use case: response packet with content arrives
const cdebug3 = jest.spyOn(console, 'debug').mockImplementation(() => {})
onDataCallback('some content')
expect(cdebug3).toHaveBeenCalledWith('Processing response content')
cdebug3.mockReset()
pair = peerFetch._requestMap.get(ticket)
expect(pair.response.receivedAll).toBeTrue()
expect(pair.response.content).toEqual('some content')
})

test('PeerFetch _dataConnection.on("open", ...)', async () => {
const dataConnection = jest.fn()
dataConnection.on = jest.fn()
const peerFetch = new PeerFetch(dataConnection)
// get the callback function registered for dataConnection.on('open')
const onOpenCallback = peerFetch._dataConnection.on.mock.calls.find(callbackDetails => callbackDetails[0] === 'open')[1]
jest.spyOn(peerFetch, '_schedulePing')
onOpenCallback()
expect(peerFetch._schedulePing).toHaveBeenCalledTimes(1)
})

test('PeerFetch _dataConnection.on("close", ...)', async () => {
const dataConnection = jest.fn()
dataConnection.on = jest.fn()
const peerFetch = new PeerFetch(dataConnection)
// get the callback function registered for dataConnection.on('close')
const onCloseCallback = peerFetch._dataConnection.on.mock.calls.find(callbackDetails => callbackDetails[0] === 'close')[1]
// first test the case when data arrives without a matching get request
jest.spyOn(peerFetch, '_stopPing')
onCloseCallback()
expect(peerFetch._stopPing).toHaveBeenCalledTimes(1)
})
2 changes: 1 addition & 1 deletion tests/unit/store/pnp-actions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ describe('PnP state machine actions - p2p communication layer', () => {
await store.dispatch(PNP_SERVICE_CONNECT)
const peer = store.state.pnp.peer
console.debug('peer.on.mock.calls', peer.on.mock.calls)
const onErrorCallback = peer.on.mock.calls.find(callbackDetails => callbackDetails[0] === 'error');
const onErrorCallback = peer.on.mock.calls.find(callbackDetails => callbackDetails[0] === 'error')
console.debug('onErrorCallback', onErrorCallback)
onErrorCallback[1]('a_network_error')
expect(store.state.pnp.pnpServiceConnectionStatus).toBe(PNP_SERVICE_DISCONNECTED)
Expand Down

0 comments on commit 3b32a32

Please sign in to comment.