Skip to content

Commit

Permalink
fix: send message to service (#838)
Browse files Browse the repository at this point in the history
Fixed issue where message sender would incorrectly return early from loop (inside `sendMessage`) even if service was unable to be satisfied by outbound transports.

Signed-off-by: Niall Shaw <[email protected]>
  • Loading branch information
niall-shaw authored Jun 8, 2022
1 parent 12cc2f8 commit 270c347
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/agent/MessageSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,10 @@ export class MessageSender {
this.logger.warn('Service does not have valid protocolScheme.')
} else if (transport.supportedSchemes.includes(protocolScheme)) {
await transport.sendMessage(outboundPackage)
break
return
}
}
throw new AriesFrameworkError(`Unable to send message to service: ${service.serviceEndpoint}`)
}

private async retrieveServicesFromDid(did: string) {
Expand Down
44 changes: 39 additions & 5 deletions packages/core/src/agent/__tests__/MessageSender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const logger = testLogger
const TransportServiceMock = TransportService as jest.MockedClass<typeof TransportService>
const DidResolverServiceMock = DidResolverService as jest.Mock<DidResolverService>

class DummyOutboundTransport implements OutboundTransport {
class DummyHttpOutboundTransport implements OutboundTransport {
public start(): Promise<void> {
throw new Error('Method not implemented.')
}
Expand All @@ -46,6 +46,22 @@ class DummyOutboundTransport implements OutboundTransport {
}
}

class DummyWsOutboundTransport implements OutboundTransport {
public start(): Promise<void> {
throw new Error('Method not implemented.')
}

public stop(): Promise<void> {
throw new Error('Method not implemented.')
}

public supportedSchemes: string[] = ['wss']

public sendMessage() {
return Promise.resolve()
}
}

describe('MessageSender', () => {
const EnvelopeService = <jest.Mock<EnvelopeServiceImpl>>(<unknown>EnvelopeServiceImpl)

Expand Down Expand Up @@ -107,7 +123,7 @@ describe('MessageSender', () => {
TransportServiceMock.mockClear()
DidResolverServiceMock.mockClear()

outboundTransport = new DummyOutboundTransport()
outboundTransport = new DummyHttpOutboundTransport()
messageRepository = new InMemoryMessageRepository(getAgentConfig('MessageSender'))
messageSender = new MessageSender(
enveloperService,
Expand All @@ -127,7 +143,9 @@ describe('MessageSender', () => {
envelopeServicePackMessageMock.mockReturnValue(Promise.resolve(encryptedMessage))
transportServiceHasInboundEndpoint.mockReturnValue(true)

const didDocumentInstance = getMockDidDocument({ service: [firstDidCommService, secondDidCommService] })
const didDocumentInstance = getMockDidDocument({
service: [firstDidCommService, secondDidCommService],
})
didResolverServiceResolveMock.mockResolvedValue(didDocumentInstance)
})

Expand Down Expand Up @@ -285,6 +303,11 @@ describe('MessageSender', () => {
expect(sendMessageToServiceSpy).toHaveBeenCalledTimes(2)
expect(sendMessageSpy).toHaveBeenCalledTimes(2)
})

test('throw error when message endpoint is not supported by outbound transport schemes', async () => {
messageSender.registerOutboundTransport(new DummyWsOutboundTransport())
await expect(messageSender.sendMessage(outboundMessage)).rejects.toThrow(/Message is undeliverable to connection/)
})
})

describe('sendMessageToService', () => {
Expand All @@ -297,7 +320,7 @@ describe('MessageSender', () => {
const senderKey = Key.fromFingerprint('z6MkmjY8GnV5i9YTDtPETC2uUAW6ejw3nk5mXF5yci5ab7th')

beforeEach(() => {
outboundTransport = new DummyOutboundTransport()
outboundTransport = new DummyHttpOutboundTransport()
messageSender = new MessageSender(
enveloperService,
transportService,
Expand Down Expand Up @@ -361,11 +384,22 @@ describe('MessageSender', () => {
})
expect(sendMessageSpy).toHaveBeenCalledTimes(1)
})

test('throw error when message endpoint is not supported by outbound transport schemes', async () => {
messageSender.registerOutboundTransport(new DummyWsOutboundTransport())
await expect(
messageSender.sendMessageToService({
message: new TestMessage(),
senderKey,
service,
})
).rejects.toThrow(/Unable to send message to service/)
})
})

describe('packMessage', () => {
beforeEach(() => {
outboundTransport = new DummyOutboundTransport()
outboundTransport = new DummyHttpOutboundTransport()
messageRepository = new InMemoryMessageRepository(getAgentConfig('PackMessage'))
messageSender = new MessageSender(
enveloperService,
Expand Down

0 comments on commit 270c347

Please sign in to comment.