-
Notifications
You must be signed in to change notification settings - Fork 198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add integration test to check inter-frame communication #4090
Merged
esanzgar
merged 2 commits into
master
from
implement-inter-frame-communication-integration-test
Jan 20, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
178 changes: 178 additions & 0 deletions
178
src/shared/test/integration/inter-frame-communication-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import { delay } from '../../../test-util/wait'; | ||
import { PortRPC as PortRPC_ } from '../../port-rpc'; | ||
import { ListenerCollection as ListenerCollection_ } from '../../listener-collection'; | ||
import { PortFinder as PortFinder_ } from '../../port-finder'; | ||
import { PortProvider as PortProvider_ } from '../../port-provider'; | ||
import { isMessageEqual } from '../../port-util'; | ||
|
||
describe('PortProvider-PortFinder-PortRPC integration', () => { | ||
let destroyables; | ||
|
||
// These proxy classes are to keep track of instances that need to be destroyed. | ||
class PortRPC extends PortRPC_ { | ||
constructor() { | ||
super(); | ||
destroyables.push(this); | ||
} | ||
} | ||
|
||
class ListenerCollection extends ListenerCollection_ { | ||
constructor() { | ||
super(); | ||
destroyables.push(this); | ||
} | ||
destroy() { | ||
this.removeAll(); | ||
} | ||
} | ||
|
||
class PortFinder extends PortFinder_ { | ||
constructor(...args) { | ||
super(...args); | ||
destroyables.push(this); | ||
} | ||
} | ||
|
||
class PortProvider extends PortProvider_ { | ||
constructor(...args) { | ||
super(...args); | ||
destroyables.push(this); | ||
} | ||
} | ||
|
||
beforeEach(() => { | ||
destroyables = []; | ||
}); | ||
|
||
afterEach(() => { | ||
destroyables.forEach(instance => instance.destroy()); | ||
}); | ||
|
||
it('enables the communication between guest-host', async () => { | ||
const simulateGuest = async () => { | ||
const portFinder = new PortFinder({ | ||
hostFrame: window, | ||
source: 'guest', | ||
}); | ||
const port = await portFinder.discover('host'); | ||
|
||
const hostRPC = new PortRPC(); | ||
hostRPC.connect(port); | ||
await new Promise(resolve => | ||
hostRPC.call('ping', response => { | ||
assert.equal(response, 'pong'); | ||
resolve(); | ||
}) | ||
); | ||
}; | ||
|
||
const simulateHost = async () => { | ||
await delay(10); // simulate scenario when host frame is ready before the guest frame | ||
|
||
const portProvider = new PortProvider(window.location.origin); | ||
portProvider.listen(); | ||
|
||
const guestRPC = new PortRPC(); | ||
guestRPC.on('ping', cb => cb('pong')); | ||
|
||
portProvider.on('frameConnected', (source, port) => { | ||
if (source === 'guest') { | ||
guestRPC.connect(port); | ||
} | ||
}); | ||
}; | ||
|
||
return Promise.all([simulateGuest(), simulateHost()]); | ||
}); | ||
|
||
it('enables the communication between guest-sidebar', async () => { | ||
const simulateGuest = async () => { | ||
const portFinder = new PortFinder({ | ||
hostFrame: window, | ||
source: 'guest', | ||
}); | ||
const hostRPC = new PortRPC(); | ||
|
||
const port = await portFinder.discover('sidebar'); | ||
hostRPC.connect(port); | ||
|
||
await new Promise(resolve => | ||
hostRPC.call('ping', response => { | ||
assert.equal(response, 'pong'); | ||
resolve(); | ||
}) | ||
); | ||
}; | ||
|
||
const simulateSidebar = async () => { | ||
const portFinder = new PortFinder({ | ||
hostFrame: window, | ||
source: 'sidebar', | ||
}); | ||
const port = await portFinder.discover('host'); | ||
|
||
const guestRPC = new PortRPC(); | ||
guestRPC.on('ping', cb => cb('pong')); | ||
|
||
const listenerCollection = new ListenerCollection(); | ||
listenerCollection.add(port, 'message', ({ data, ports }) => { | ||
if ( | ||
isMessageEqual(data, { | ||
frame1: 'guest', | ||
frame2: 'sidebar', | ||
type: 'offer', | ||
}) | ||
) { | ||
guestRPC.connect(ports[0]); | ||
} | ||
}); | ||
|
||
port.start(); // `start` is normally invoked by `hostRPC.connect(port)` | ||
}; | ||
|
||
const simulateHost = async () => { | ||
await delay(10); // simulate scenario when host frame is ready before the guest frame | ||
|
||
const portProvider = new PortProvider(window.location.origin); | ||
portProvider.listen(); | ||
}; | ||
|
||
return Promise.all([simulateGuest(), simulateSidebar(), simulateHost()]); | ||
}); | ||
|
||
it('enables the communication between sidebar-host', async () => { | ||
const simulateSidebar = async () => { | ||
const portFinder = new PortFinder({ | ||
hostFrame: window, | ||
source: 'sidebar', | ||
}); | ||
const port = await portFinder.discover('host'); | ||
|
||
const hostRPC = new PortRPC(); | ||
hostRPC.connect(port); | ||
|
||
await new Promise(resolve => { | ||
hostRPC.call('ping', response => { | ||
assert.equal(response, 'pong'); | ||
resolve(); | ||
}); | ||
}); | ||
}; | ||
|
||
const simulateHost = () => { | ||
const portProvider = new PortProvider(window.location.origin); | ||
portProvider.listen(); | ||
|
||
const sidebarRPC = new PortRPC(); | ||
sidebarRPC.on('ping', cb => cb('pong')); | ||
|
||
portProvider.on('frameConnected', (source, port) => { | ||
if (source === 'sidebar') { | ||
sidebarRPC.connect(port); | ||
} | ||
}); | ||
}; | ||
|
||
return Promise.all([simulateSidebar(), simulateHost()]); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To summarize what these tests are doing, each runs a bunch of concurrent activities that simulate the guest, host and sidebar roles in the inter-frame communication process. All the only communication between frames has to go via message channels (no shared variables). One way to make this structure more obvious might be to wrap each "actor" in a function and then run all the actors concurrently at the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good idea! Thanks!