Skip to content
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

feat: implement unsubscribe by id #3013

Merged
merged 2 commits into from
Jan 22, 2025
Merged
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
7 changes: 7 additions & 0 deletions src/bidiMapper/BidiNoOpParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ export class BidiNoOpParser implements BidiCommandParameterParser {
parseSubscribeParams(params: unknown): Session.SubscriptionRequest {
return params as Session.SubscriptionRequest;
}
parseUnsubscribeParams(
params: unknown,
): Session.UnsubscribeByAttributesRequest | Session.UnsubscribeByIdRequest {
return params as
| Session.UnsubscribeByAttributesRequest
| Session.UnsubscribeByIdRequest;
}
// keep-sorted end

// Storage module
Expand Down
1 change: 1 addition & 0 deletions src/bidiMapper/BidiParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export interface BidiCommandParameterParser {
// Session module
// keep-sorted start block=yes
parseSubscribeParams(params: unknown): Session.SubscriptionRequest;
parseUnsubscribeParams(params: unknown): Session.UnsubscribeParameters;
// keep-sorted end

// Storage module
Expand Down
2 changes: 1 addition & 1 deletion src/bidiMapper/CommandProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export class CommandProcessor extends EventEmitter<CommandProcessorEventsMap> {
);
case 'session.unsubscribe':
return await this.#sessionProcessor.unsubscribe(
this.#parser.parseSubscribeParams(command.params),
this.#parser.parseUnsubscribeParams(command.params),
command.channel,
);
// keep-sorted end
Expand Down
5 changes: 5 additions & 0 deletions src/bidiMapper/modules/session/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ export class EventManager extends EventEmitter<EventManagerEventsMap> {
await this.toggleModulesIfNeeded();
}

async unsubscribeByIds(subscriptionIds: string[]): Promise<void> {
this.#subscriptionManager.unsubscribeById(subscriptionIds);
await this.toggleModulesIfNeeded();
}

async toggleModulesIfNeeded(): Promise<void> {
// TODO(1): Only update changed subscribers
// TODO(2): Enable for Worker Targets
Expand Down
6 changes: 5 additions & 1 deletion src/bidiMapper/modules/session/SessionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,13 @@ export class SessionProcessor {
}

async unsubscribe(
params: Session.SubscriptionRequest,
params: Session.UnsubscribeParameters,
channel: BidiPlusChannel = {},
): Promise<EmptyResult> {
if ('subscriptions' in params) {
await this.#eventManager.unsubscribeByIds(params.subscriptions);
return {};
}
await this.#eventManager.unsubscribe(
params.events as ChromiumBidi.EventNames[],
params.contexts ?? [],
Expand Down
64 changes: 64 additions & 0 deletions src/bidiMapper/modules/session/SubscriptionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ describe('SubscriptionManager', () => {
).to.equal(false);
});

it('should unsubscribe by id', () => {
const {id} = subscriptionManager.subscribe(
[SOME_EVENT],
[],
SOME_CHANNEL,
);
subscriptionManager.unsubscribeById([id]);
expect(
subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT),
).to.equal(false);
});

it('should not unsubscribe on error', () => {
subscriptionManager.subscribe([SOME_EVENT], [], SOME_CHANNEL);
expect(() =>
Expand Down Expand Up @@ -299,6 +311,18 @@ describe('SubscriptionManager', () => {
).to.equal(false);
});

it('should unsubscribe by id', () => {
const {id} = subscriptionManager.subscribe(
[SOME_EVENT],
[SOME_CONTEXT],
SOME_CHANNEL,
);
subscriptionManager.unsubscribeById([id]);
expect(
subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT),
).to.equal(false);
});

it('should partially unsubscribe from a context', () => {
subscriptionManager.subscribe(
[SOME_EVENT],
Expand Down Expand Up @@ -430,6 +454,46 @@ describe('SubscriptionManager', () => {
});
});

describe('unsubscribeById', () => {
it('should keep subscription if one of the IDs is not known', () => {
const {id} = subscriptionManager.subscribe(
[SOME_EVENT],
[],
SOME_CHANNEL,
);
expect(
subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT),
).to.equal(true);
expect(() => {
subscriptionManager.unsubscribeById([id, 'wrong']);
}).to.throw('No subscription found');
expect(
subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT),
).to.equal(true);
});

it('should throw an error if an ID is not know', () => {
expect(() => {
subscriptionManager.unsubscribeById(['wrong']);
}).to.throw('No subscription found');
});

it('should throw an error if a subscription is used multiple times', () => {
const {id} = subscriptionManager.subscribe(
[SOME_EVENT],
[],
SOME_CHANNEL,
);
expect(
subscriptionManager.isSubscribedTo(SOME_EVENT, SOME_CONTEXT),
).to.equal(true);
subscriptionManager.unsubscribeById([id]);
expect(() => {
subscriptionManager.unsubscribeById([id]);
}).to.throw('No subscription found');
});
});

describe('cartesian product', () => {
it('should return empty array for empty array', () => {
expect(cartesianProduct([], [])).to.deep.equal([]);
Expand Down
21 changes: 19 additions & 2 deletions src/bidiMapper/modules/session/SubscriptionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export type Subscription = {

export class SubscriptionManager {
#subscriptions: Subscription[] = [];
#knownSubscriptionIds = new Set<string>();
#browsingContextStorage: BrowsingContextStorage;

constructor(browsingContextStorage: BrowsingContextStorage) {
Expand Down Expand Up @@ -222,6 +223,7 @@ export class SubscriptionManager {
channel,
};
this.#subscriptions.push(subscription);
this.#knownSubscriptionIds.add(subscription.id);
return subscription;
}

Expand Down Expand Up @@ -349,8 +351,23 @@ export class SubscriptionManager {
/**
* Unsubscribes by subscriptionId.
*/
unsubscribeById(_subscription: string) {
// TODO: implement.
unsubscribeById(subscriptionIds: string[]) {
OrKoN marked this conversation as resolved.
Show resolved Hide resolved
const subscriptionIdsSet = new Set(subscriptionIds);
const unknownIds = difference(
subscriptionIdsSet,
this.#knownSubscriptionIds,
);

if (unknownIds.size !== 0) {
throw new InvalidArgumentException('No subscription found');
}
this.#subscriptions = this.#subscriptions.filter((subscription) => {
return !subscriptionIdsSet.has(subscription.id);
});
this.#knownSubscriptionIds = difference(
this.#knownSubscriptionIds,
subscriptionIdsSet,
);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/bidiTab/BidiParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ export class BidiParser implements BidiCommandParameterParser {
parseSubscribeParams(params: unknown): Session.SubscriptionRequest {
return Parser.Session.parseSubscribeParams(params);
}
parseUnsubscribeParams(params: unknown): Session.UnsubscribeParameters {
return Parser.Session.parseUnsubscribeParams(params);
}
// keep-sorted end

// Storage module
Expand Down
9 changes: 9 additions & 0 deletions src/protocol-parser/protocol-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ export namespace Session {
WebDriverBidi.Session.SubscriptionRequestSchema,
) as Protocol.Session.SubscriptionRequest;
}

export function parseUnsubscribeParams(
params: unknown,
): Protocol.Session.UnsubscribeParameters {
return parseObject(
params,
WebDriverBidi.Session.UnsubscribeParametersSchema,
) as Protocol.Session.UnsubscribeParameters;
}
}

export namespace Input {
Expand Down
12 changes: 12 additions & 0 deletions tests/session/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,18 @@ async def test_subscribeWithoutContext_bufferedEventsFromNotClosedContextsAreRet
assert {"type": "success", "id": command_id, 'result': ANY} == resp


@pytest.mark.asyncio
async def test_unsubscribe_by_id(websocket):
res = await subscribe(websocket, ["log.entryAdded"])
await execute_command(
websocket, {
"method": "session.unsubscribe",
"params": {
"subscriptions": [res["subscription"]]
}
})


@pytest.mark.asyncio
@pytest.mark.parametrize("channel_name", ["channel", "goog:channel"])
async def test_unsubscribeIsAtomic(websocket, context_id, iframe_id,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async def subscribe(websocket,
command[channel_name
if channel_name is not None else "goog:channel"] = channel

await execute_command(websocket, command)
return await execute_command(websocket, command)


async def send_JSON_command(websocket, command: dict) -> int:
Expand Down
Loading