Skip to content

Commit

Permalink
feat: topic next message tests
Browse files Browse the repository at this point in the history
  • Loading branch information
markuczy committed Jun 3, 2024
1 parent f07e82b commit cf5c897
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 29 deletions.
16 changes: 6 additions & 10 deletions libs/accelerator/src/lib/topic/topic-message.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { Message } from "./message";
import { TopicMessageType } from "./topic-message-type";
import { Message } from './message'
import { TopicMessageType } from './topic-message-type'

export class TopicMessage extends Message {
constructor(
type: TopicMessageType,
public name: string,
public version: number
) {
super(type);
}
}
constructor(type: TopicMessageType, public name: string, public version: number) {
super(type)
}
}
129 changes: 129 additions & 0 deletions libs/accelerator/src/lib/topic/topic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import { map } from 'rxjs'
import { Topic } from './topic'
import { TopicMessageType } from './topic-message-type'
import { TopicDataMessage } from './topic-data-message'
import { TopicMessage } from './topic-message'

Check failure on line 13 in libs/accelerator/src/lib/topic/topic.spec.ts

View workflow job for this annotation

GitHub Actions / main

'TopicMessage' is defined but never used

describe('Topic', () => {
const origAddEventListener = window.addEventListener
Expand Down Expand Up @@ -155,4 +158,130 @@ describe('Topic', () => {
done()
})
})

describe('on message', () => {
let currentMessage: TopicDataMessage<string>
let incomingMessage: MessageEvent<TopicDataMessage<string>>

beforeEach(() => {
currentMessage = {
type: TopicMessageType.TopicNext,
name: testTopic1.name,
version: testTopic1.version,
data: '',
timestamp: 0,
id: 0,
}
incomingMessage = {
data: {
type: TopicMessageType.TopicNext,
name: testTopic1.name,
version: testTopic1.version,
data: '',
timestamp: 0,
id: 0,
},
} as any

// initialize topic
testTopic1.publish('initMsg')
})

it('should have value if incoming id is greater than previous id', () => {
currentMessage.data = 'msg1'
currentMessage.id = 0
incomingMessage.data.data = 'msg2'
incomingMessage.data.id = 1

testTopic1.onMessage(currentMessage, incomingMessage)

expect(values1).toEqual(['initMsg', 'msg2'])
})

it('should have value if incoming timestamp is greater than previous timestamp with no ids provided', () => {
currentMessage.data = 'msg1'
currentMessage.id = undefined!

Check warning on line 203 in libs/accelerator/src/lib/topic/topic.spec.ts

View workflow job for this annotation

GitHub Actions / main

Forbidden non-null assertion
currentMessage.timestamp = 1
incomingMessage.data.data = 'msg2'
incomingMessage.data.id = undefined!

Check warning on line 206 in libs/accelerator/src/lib/topic/topic.spec.ts

View workflow job for this annotation

GitHub Actions / main

Forbidden non-null assertion
incomingMessage.data.timestamp = 3

testTopic1.onMessage(currentMessage, incomingMessage)

expect(values1).toEqual(['initMsg', 'msg2'])
})

it('should have value if incoming timestamp is greater than previous timestamp when current message has id', () => {
currentMessage.data = 'msg1'
currentMessage.id = 1
currentMessage.timestamp = 1
incomingMessage.data.data = 'msg2'
incomingMessage.data.id = undefined!

Check warning on line 219 in libs/accelerator/src/lib/topic/topic.spec.ts

View workflow job for this annotation

GitHub Actions / main

Forbidden non-null assertion
incomingMessage.data.timestamp = 3

testTopic1.onMessage(currentMessage, incomingMessage)

expect(values1).toEqual(['initMsg', 'msg2'])
})

it('should have value if incoming timestamp is greater than previous timestamp when incoming message has id', () => {
currentMessage.data = 'msg1'
currentMessage.id = undefined!

Check warning on line 229 in libs/accelerator/src/lib/topic/topic.spec.ts

View workflow job for this annotation

GitHub Actions / main

Forbidden non-null assertion
currentMessage.timestamp = 1
incomingMessage.data.data = 'msg2'
incomingMessage.data.id = 1
incomingMessage.data.timestamp = 3

testTopic1.onMessage(currentMessage, incomingMessage)

expect(values1).toEqual(['initMsg', 'msg2'])
})

it('should have no value if incoming timestamp is equal to the previous timestamp with no ids provided', () => {
currentMessage.data = 'msg1'
currentMessage.id = undefined!

Check warning on line 242 in libs/accelerator/src/lib/topic/topic.spec.ts

View workflow job for this annotation

GitHub Actions / main

Forbidden non-null assertion
currentMessage.timestamp = 3
incomingMessage.data.data = 'msg2'
incomingMessage.data.id = undefined!

Check warning on line 245 in libs/accelerator/src/lib/topic/topic.spec.ts

View workflow job for this annotation

GitHub Actions / main

Forbidden non-null assertion
incomingMessage.data.timestamp = 3

testTopic1.onMessage(currentMessage, incomingMessage)

expect(values1).toEqual(['initMsg'])
})

it('should have no value if incoming timestamp is equal to the previous timestamp when current message has id', () => {
jest.spyOn(console, 'warn')
currentMessage.data = 'msg1'
currentMessage.id = 1
currentMessage.timestamp = 3
incomingMessage.data.data = 'msg2'
incomingMessage.data.id = undefined!

Check warning on line 259 in libs/accelerator/src/lib/topic/topic.spec.ts

View workflow job for this annotation

GitHub Actions / main

Forbidden non-null assertion
incomingMessage.data.timestamp = 3

testTopic1.onMessage(currentMessage, incomingMessage)

expect(values1).toEqual(['initMsg'])
expect(console.warn).toHaveBeenLastCalledWith(
'Message was swallowed because of equal timestamps. Please upgrate to the latest version to ensure messages are correctly timed'
)
})

it('should have no value if incoming timestamp is equal to previous timestamp when incoming message has id', () => {
jest.spyOn(console, 'warn')
currentMessage.data = 'msg1'
currentMessage.id = undefined!

Check warning on line 273 in libs/accelerator/src/lib/topic/topic.spec.ts

View workflow job for this annotation

GitHub Actions / main

Forbidden non-null assertion
currentMessage.timestamp = 3
incomingMessage.data.data = 'msg2'
incomingMessage.data.id = 1
incomingMessage.data.timestamp = 3

testTopic1.onMessage(currentMessage, incomingMessage)

expect(values1).toEqual(['initMsg'])
expect(console.warn).toHaveBeenLastCalledWith(
'Message was swallowed because of equal timestamps. Please upgrate to the latest version to ensure messages are correctly timed'
)
})
})
})
43 changes: 24 additions & 19 deletions libs/accelerator/src/lib/topic/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Topic<T> extends TopicPublisher<T> implements Subscribable<T> {

protected isInit = false
private resolveInitPromise!: (value: void | PromiseLike<void>) => void
private eventListener = (m: MessageEvent<TopicMessage>) => this.onMessage(m)
private eventListener = (m: MessageEvent<TopicMessage>) => this.onMessage(this.data.value, m)

constructor(name: string, version: number, sendGetMessage = true) {
super(name, version)
Expand Down Expand Up @@ -142,45 +142,50 @@ export class Topic<T> extends TopicPublisher<T> implements Subscribable<T> {
window.removeEventListener('message', this.eventListener, true)
}

private onMessage(m: MessageEvent<TopicMessage>): any {
switch (m.data.type) {
public onMessage(currentMessage: TopicDataMessage<T> | undefined, incomingMessage: MessageEvent<TopicMessage>): any {
switch (incomingMessage.data.type) {
case TopicMessageType.TopicNext:
if (m.data.name !== this.name || m.data.version !== this.version) {
if (incomingMessage.data.name !== this.name || incomingMessage.data.version !== this.version) {
break
}

if (
!this.data.value ||
!currentMessage ||
(this.isInit &&
(<TopicMessage>m.data).id &&
this.data.value.id &&
(<TopicMessage>m.data).id > this.data.value.id) ||
(this.isInit && (<TopicMessage>m.data).timestamp > this.data.value.timestamp)
(<TopicMessage>incomingMessage.data).id !== undefined &&
currentMessage.id !== undefined &&
(<TopicMessage>incomingMessage.data).id > currentMessage.id) ||
(this.isInit && (<TopicMessage>incomingMessage.data).timestamp > currentMessage.timestamp)
) {
this.isInit = true
this.data.next(<TopicDataMessage<T>>m.data)
this.data.next(<TopicDataMessage<T>>incomingMessage.data)
this.resolveInitPromise()
const publishPromiseResolver = this.publishPromiseResolver[m.data.timestamp]
const publishPromiseResolver = this.publishPromiseResolver[incomingMessage.data.timestamp]
if (publishPromiseResolver) {
publishPromiseResolver()
delete this.publishPromiseResolver[m.data.timestamp]
delete this.publishPromiseResolver[incomingMessage.data.timestamp]
}
} else if (
this.data.value &&
currentMessage &&
this.isInit &&
(<TopicMessage>m.data).timestamp === this.data.value.timestamp &&
((<TopicMessage>m.data).id || this.data.value.id)
(<TopicMessage>incomingMessage.data).timestamp === currentMessage.timestamp &&
((<TopicMessage>incomingMessage.data).id || currentMessage.id)
) {
console.warn(
'Message was swallowed because of equal timestamps. Please upgrate to the latest version to ensure messages are correctly timed'
)
}
break
case TopicMessageType.TopicGet:
if (m.data.name === this.name && m.data.version === this.version && this.isInit && this.data.value) {
window.postMessage(this.data.value, '*')
m.stopImmediatePropagation()
m.stopPropagation()
if (
incomingMessage.data.name === this.name &&
incomingMessage.data.version === this.version &&
this.isInit &&
currentMessage
) {
window.postMessage(currentMessage, '*')
incomingMessage.stopImmediatePropagation()
incomingMessage.stopPropagation()
}
break
}
Expand Down

0 comments on commit cf5c897

Please sign in to comment.