Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
});
// Send a shell message with the wrong client (parent) session.
const msg1 = KernelMessage.createMessage({
msgType: 'kernel_info_request',
channel: 'shell',
session: tester.serverSessionId,
msgId: 'message from wrong session',
content: {}
});
msg1.parent_header = { session: 'wrong session' };
tester.send(msg1);
// Send a shell message with the right client (parent) session.
const msg2 = KernelMessage.createMessage({
msgType: 'kernel_info_request',
channel: 'shell',
session: tester.serverSessionId,
msgId: msgId,
content: {}
});
msg2.parent_header = { session: kernel.clientId };
tester.send(msg2);
await emission;
});
});
it('should throw on missing iopub message content', () => {
const msg = KernelMessage.createMessage({
msgType: 'error',
channel: 'iopub',
session: 'baz',
content: {} as any
} as any);
expect(() => validateMessage(msg)).to.throw();
});
it('should be emitted regardless of the sender', async () => {
const tester = new KernelTester();
const kernel = await tester.start();
const msgId = UUID.uuid4();
const emission = testEmission(kernel.iopubMessage, {
find: (k, msg) => msg.header.msg_id === msgId
});
const msg = KernelMessage.createMessage({
msgType: 'status',
channel: 'iopub',
session: tester.serverSessionId,
msgId,
content: {
execution_state: 'idle'
}
});
tester.send(msg);
await emission;
await tester.shutdown();
tester.dispose();
});
});
it('should check for an execute result message type', () => {
let msg = KernelMessage.createMessage({
msgType: 'execute_result',
channel: 'iopub',
session: 'baz',
content: { data: {}, execution_count: 1, metadata: {} }
});
expect(KernelMessage.isExecuteResultMsg(msg)).to.equal(true);
expect(KernelMessage.isExecuteResultMsg(iopubStatusMsg)).to.equal(false);
});
});
it('should check for a stream message type', () => {
let msg = KernelMessage.createMessage({
msgType: 'stream',
channel: 'iopub',
session: 'baz',
content: {
name: 'stdout',
text: 'hello world'
}
});
expect(KernelMessage.isStreamMsg(msg)).to.equal(true);
expect(KernelMessage.isStreamMsg(iopubStatusMsg)).to.equal(false);
});
});
it('should be emitted for an unhandled message', async () => {
const kernel = await tester.start();
const msgId = UUID.uuid4();
const emission = testEmission(kernel.anyMessage, {
test: (k, args) => {
expect(args.msg.header.msg_id).to.equal(msgId);
expect(args.msg.header.msg_type).to.equal('kernel_info_request');
expect(args.direction).to.equal('recv');
}
});
const msg = KernelMessage.createMessage({
msgType: 'kernel_info_request',
channel: 'shell',
session: tester.serverSessionId,
msgId,
content: {}
});
msg.parent_header = { session: kernel.clientId };
tester.send(msg);
await emission;
});
it('should check for a status message type', () => {
let msg = KernelMessage.createMessage({
msgType: 'status',
channel: 'iopub',
session: 'baz',
content: {
execution_state: 'idle'
}
});
expect(KernelMessage.isStatusMsg(msg)).to.equal(true);
let msg2 = KernelMessage.createMessage({
msgType: 'execute_input',
channel: 'iopub',
session: 'baz',
content: {
code: '',
execution_count: 1
}
});
expect(KernelMessage.isStatusMsg(msg2)).to.equal(false);
});
});
it('should throw if missing a field', () => {
const msg = KernelMessage.createMessage({
msgType: 'comm_msg',
channel: 'iopub',
session: 'baz',
content: { comm_id: 'foo', data: {} }
});
delete msg.channel;
expect(() => validateMessage(msg)).to.throw();
});
sendStatus(status: Kernel.Status, parentHeader?: KernelMessage.IHeader) {
const msg = KernelMessage.createMessage({
msgType: 'status',
channel: 'iopub',
session: this.serverSessionId,
content: {
execution_state: status
}
});
if (parentHeader) {
msg.parent_header = parentHeader;
}
this.send(msg);
}
sendStatus(status: Kernel.Status, parentHeader?: KernelMessage.IHeader) {
const msg = KernelMessage.createMessage({
msgType: 'status',
channel: 'iopub',
session: this.serverSessionId,
content: {
execution_state: status
}
});
if (parentHeader) {
msg.parent_header = parentHeader;
}
this.send(msg);
}