Skip to content

Commit faa34f6

Browse files
edvilmeCopilot
andcommitted
fix: convert all setEnvironment waits to event-driven drain handlers
Consistently use TestEventHandler drain pattern for all tests that call setEnvironment, not just the two that were failing. This eliminates waitForCondition polling (100ms intervals) in favor of event-driven resolution (~1 setImmediate tick). Remaining waitForCondition uses are appropriate: - extension.isActive: no event API available, must poll - handler.all.some(): already event-driven (searching collected events) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent dd21d7b commit faa34f6

1 file changed

Lines changed: 32 additions & 41 deletions

File tree

src/test/integration/interpreterSelection.integration.test.ts

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,23 @@ suite('Integration: Interpreter Selection Priority', function () {
9696

9797
const envToSet = environments[0];
9898

99+
// Subscribe before setting so we can wait for the event to propagate.
100+
const drainHandler = new TestEventHandler<DidChangeEnvironmentEventArgs>(
101+
api.onDidChangeEnvironment,
102+
'drain',
103+
);
104+
99105
// Set environment globally
100106
await api.setEnvironment(undefined, envToSet);
107+
await drainHandler.assertFired(15_000);
108+
drainHandler.dispose();
101109

102-
// Wait for the async config write to propagate and verify the result.
103-
// setEnvironment fires onDidChangeEnvironment asynchronously, so getEnvironment
104-
// called immediately after may still return the previous (auto-discovered) value
105-
// on slower CI runners.
106-
let retrieved: PythonEnvironment | undefined;
107-
await waitForCondition(
108-
async () => {
109-
retrieved = await api.getEnvironment(undefined);
110-
return !!retrieved && retrieved.environmentPath.fsPath === envToSet.environmentPath.fsPath;
111-
},
112-
15_000,
113-
() => `Environment was not persisted as ${envToSet.environmentPath.fsPath}`,
114-
);
110+
// Get and verify
111+
const retrieved = await api.getEnvironment(undefined);
115112

116113
assert.ok(retrieved, 'Should have environment after setting');
117114
assert.strictEqual(
118-
retrieved!.environmentPath.fsPath,
115+
retrieved.environmentPath.fsPath,
119116
envToSet.environmentPath.fsPath,
120117
'Retrieved environment should point to the same interpreter as the one set',
121118
);
@@ -140,47 +137,41 @@ suite('Integration: Interpreter Selection Priority', function () {
140137
const projectEnv = environments[1];
141138
const project = projects[0];
142139

140+
// Subscribe before setting so we can wait for events to propagate.
141+
const globalDrain = new TestEventHandler<DidChangeEnvironmentEventArgs>(
142+
api.onDidChangeEnvironment,
143+
'globalDrain',
144+
);
145+
143146
// Set global environment
144147
await api.setEnvironment(undefined, globalEnv);
148+
await globalDrain.assertFired(15_000);
149+
globalDrain.dispose();
150+
151+
const projectDrain = new TestEventHandler<DidChangeEnvironmentEventArgs>(
152+
api.onDidChangeEnvironment,
153+
'projectDrain',
154+
);
145155

146156
// Set different environment for project
147157
await api.setEnvironment(project.uri, projectEnv);
158+
await projectDrain.assertFired(15_000);
159+
projectDrain.dispose();
148160

149-
// Wait for the global env async write to propagate and verify.
150-
let globalRetrieved: PythonEnvironment | undefined;
151-
await waitForCondition(
152-
async () => {
153-
globalRetrieved = await api.getEnvironment(undefined);
154-
return !!globalRetrieved && globalRetrieved.environmentPath.fsPath === globalEnv.environmentPath.fsPath;
155-
},
156-
15_000,
157-
() => `Global environment was not persisted as ${globalEnv.environmentPath.fsPath}`,
158-
);
159-
161+
// Verify global is unchanged
162+
const globalRetrieved = await api.getEnvironment(undefined);
160163
assert.ok(globalRetrieved, 'Global should have environment');
161164
assert.strictEqual(
162-
globalRetrieved!.environmentPath.fsPath,
165+
globalRetrieved.environmentPath.fsPath,
163166
globalEnv.environmentPath.fsPath,
164167
'Global selection should be unchanged',
165168
);
166169

167-
// Wait for the project env async write to propagate and verify.
168-
let projectRetrieved: PythonEnvironment | undefined;
169-
await waitForCondition(
170-
async () => {
171-
projectRetrieved = await api.getEnvironment(project.uri);
172-
return (
173-
!!projectRetrieved &&
174-
projectRetrieved.environmentPath.fsPath === projectEnv.environmentPath.fsPath
175-
);
176-
},
177-
15_000,
178-
() => `Project environment was not persisted as ${projectEnv.environmentPath.fsPath}`,
179-
);
180-
170+
// Verify project has its own selection
171+
const projectRetrieved = await api.getEnvironment(project.uri);
181172
assert.ok(projectRetrieved, 'Project should have environment');
182173
assert.strictEqual(
183-
projectRetrieved!.environmentPath.fsPath,
174+
projectRetrieved.environmentPath.fsPath,
184175
projectEnv.environmentPath.fsPath,
185176
'Project should have its own selection',
186177
);

0 commit comments

Comments
 (0)