Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/services/monitoring/monitoring-base.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'mocha'
import { expect } from 'chai'
import { EVENT_ACTION, TOPIC } from '../../constants'
import { MonitoringBase } from './monitoring-base'

class TestMonitoring extends MonitoringBase {
public description = 'test monitoring'
}

const services: any = {
clusterRegistry: { getAll: () => ['server-a'] },
clusterStates: { getStateRegistries: () => new Map() }
}

const emit = { topic: TOPIC.EVENT, action: EVENT_ACTION.EMIT, name: 'someName' } as any
const subscribe = { topic: TOPIC.EVENT, action: EVENT_ACTION.SUBSCRIBE, name: 'someName' } as any

describe('monitoring base', () => {
let monitoring: TestMonitoring

beforeEach(() => {
monitoring = new TestMonitoring(services)
})

it('counts broadcasts as sent, not received', () => {
monitoring.onMessageReceived(emit, null as any)
monitoring.onBroadcast(emit, 2)

const stats = monitoring.getAndResetMonitoringStats()
expect(stats.received).to.deep.equal({ EVENT: { EMIT: 1 } })
expect(stats.send).to.deep.equal({ EVENT: { EMIT: 2 } })
})

it('adds broadcasts to what was already sent on the topic', () => {
monitoring.onMessageSend(subscribe)
monitoring.onBroadcast(emit, 3)
monitoring.onBroadcast(emit, 2)

const stats = monitoring.getAndResetMonitoringStats()
expect(stats.received).to.deep.equal({})
expect(stats.send).to.deep.equal({ EVENT: { SUBSCRIBE: 1, EMIT: 5 } })
})

it('resets the counters after reading them', () => {
monitoring.onBroadcast(emit, 2)
monitoring.getAndResetMonitoringStats()

const stats = monitoring.getAndResetMonitoringStats()
expect(stats.send).to.deep.equal({})
expect(stats.clusterSize).to.equal(1)
})
})
2 changes: 1 addition & 1 deletion src/services/monitoring/monitoring-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export abstract class MonitoringBase extends DeepstreamPlugin implements Deepstr
}

public onBroadcast (message: Message, count: number): void {
let actionsMap = this.receiveStats[TOPIC[message.topic]]
let actionsMap = this.sendStats[TOPIC[message.topic]]
if (!actionsMap) {
actionsMap = {}
this.sendStats[TOPIC[message.topic]] = actionsMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ describe('subscription registry', () => {
subscriptionRegistry.sendToSubscribers('someName', eventMessage, false, clientA.socketWrapper)
})

it('reports only the subscribers it sends to to monitoring', () => {
subscriptionRegistry.subscribe(subscribeMessage.name, subscribeMessage, clientA.socketWrapper)
subscriptionRegistry.subscribe(subscribeMessage.name, subscribeMessage, clientB.socketWrapper)

clientB.socketWrapperMock
.expects('sendBuiltMessage')
.once()

const onBroadcast = sinon.spy(services.monitoring!, 'onBroadcast')
try {
subscriptionRegistry.sendToSubscribers('someName', eventMessage, false, clientA.socketWrapper)
expect(onBroadcast).to.have.been.calledOnceWithExactly(eventMessage, 1)
} finally {
onBroadcast.restore()
}
})

it('unsubscribes', () => {
subscriptionRegistry.subscribe(subscribeMessage.name, subscribeMessage, clientA.socketWrapper)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ export class DefaultSubscriptionRegistry implements SubscriptionRegistry {

const subscribers = subscription.sockets

this.services.monitoring.onBroadcast(message, subscribers.size)
// The sender is skipped below, so it is not one of the sockets the message goes to
const recipients = senderSocket !== null && subscribers.has(senderSocket) ? subscribers.size - 1 : subscribers.size
this.services.monitoring.onBroadcast(message, recipients)

const serializedMessages: { [index: string]: any } = {}
for (const socket of subscribers) {
Expand Down