Skip to content

Unable to get the result of an asynchronous function in a websocket code block #5524

@sergeylysov

Description

@sergeylysov

Brief summary

When attempting to call an asynchronous function from websocket functions, no result is returned.
This is necessary to use functions from the crypto.subtle package.

k6 version

v1.4.2

OS

linux/amd64, Manjaro

Docker version and image (if applicable)

No response

Steps to reproduce the problem

Script for playback problem


import ws from 'k6/ws';
import { sleep } from 'k6';
export async function async_func(){
    console.log("start async_func")
    sleep(1);
    console.log("end async_func")
    return 10;
}

export default async function () {
    const url = 'wss://test-ws-address:443/api';
    const params = { tags: { my_tag: 'hello' } };
    var res = await async_func();
    console.log('res async function outer websocket = '+res);
    const res_ws = ws.connect(url, params, async function (socket) {
        socket.on('open', function open() {
            console.log('connected');
            socket.setInterval( function timeout() {
                socket.ping();
                console.log('Pinging every 3sec (setInterval test)');
            }, 3000);
        });
        socket.on('ping', () => console.log('PING!'));
        socket.on('pong', async () => {
            console.log('PONG!');
        var res1 = await async_func();
        console.log('res async function in websocket = '+res1);
        });
        socket.on('close', () => console.log('disconnected'));
        socket.on('error', function (e) {
            if (e.error() != 'websocket: close sent') {
                console.log('An unexpected error occured: ', e.error());
            }
        });
    });

}

Calling an asynchronous function in the main code block returns a result, but in a websocket code block it does not.

Expected behaviour

Calling an asynchronous function in the main code block returns a result, but in a websocket code block it does not.

INFO[0000] start async_func source=console
INFO[0001] end async_func source=console
INFO[0001] res async function outer websocket = 10 source=console
INFO[0001] connected source=console
INFO[0004] Pinging every 3sec (setInterval test) source=console
INFO[0004] PONG! source=console
INFO[0004] start async_func source=console
INFO[0005] end async_func source=console
INFO[0007] Pinging every 3sec (setInterval test) source=console
INFO[0007] PONG! source=console
INFO[0007] start async_func source=console
INFO[0008] end async_func source=console

Actual behaviour

Calling an asynchronous function from a websocket block returns a result.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions