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: STALE state, minor event changes #541

Merged
merged 7 commits into from
Sep 20, 2023
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
4 changes: 2 additions & 2 deletions packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<!-- x-hide-in-docs-end -->
<!-- The 'github-badges' class is used in the docs -->
<p align="center" class="github-badges">
<a href="https://github.com/open-feature/spec/tree/v0.6.0">
<img alt="Specification" src="https://img.shields.io/static/v1?label=specification&message=v0.6.0&color=yellow&style=for-the-badge" />
<a href="https://github.com/open-feature/spec/tree/v0.7.0">
<img alt="Specification" src="https://img.shields.io/static/v1?label=specification&message=v0.7.0&color=yellow&style=for-the-badge" />
</a>
<!-- x-release-please-start-version -->
<a href="https://github.com/open-feature/js-sdk/releases/tag/web-sdk-v0.4.0">
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/client/open-feature-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Client } from './client';
import { Provider } from '../provider';
import {
ClientMetadata,
ErrorCode,
Expand All @@ -20,8 +18,10 @@ import {
SafeLogger,
StandardResolutionReasons,
} from '@openfeature/shared';
import { OpenFeature } from '../open-feature';
import { FlagEvaluationOptions } from '../evaluation';
import { OpenFeature } from '../open-feature';
import { Provider } from '../provider';
import { Client } from './client';

type OpenFeatureClientOptions = {
name?: string;
Expand Down Expand Up @@ -56,7 +56,7 @@ export class OpenFeatureClient implements Client {
if (eventType === ProviderEvents.Ready && providerReady) {
// run immediately, we're ready.
try {
handler({ clientName: this.metadata.name });
handler({ clientName: this.metadata.name, providerName: this._provider.metadata.name });
} catch (err) {
this._logger?.error('Error running event handler:', err);
}
Expand Down
12 changes: 5 additions & 7 deletions packages/client/test/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
Client,
Provider,
ErrorCode,
EvaluationDetails,
JsonValue,
FlagNotFoundError,
JsonArray,
JsonObject,
JsonValue,
OpenFeature,
OpenFeatureClient,
Provider,
ResolutionDetails,
StandardResolutionReasons,
FlagNotFoundError,
OpenFeatureClient,
OpenFeature,
} from '../src';

const BOOLEAN_VALUE = true;
Expand Down Expand Up @@ -46,7 +46,6 @@ const MOCK_PROVIDER: Provider = {
metadata: {
name: 'mock',
},

events: undefined,
hooks: [],
initialize(): Promise<void> {
Expand Down Expand Up @@ -370,7 +369,6 @@ describe('Evaluation details structure', () => {
metadata: {
name: 'error-mock',
},

resolveNumberEvaluation: jest.fn((): Promise<ResolutionDetails<number>> => {
throw new Error(NON_OPEN_FEATURE_ERROR_MESSAGE); // throw a non-open-feature error
}),
Expand Down
76 changes: 45 additions & 31 deletions packages/client/test/events.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { v4 as uuid } from 'uuid';
import {
JsonValue,
NOOP_PROVIDER,
Expand All @@ -10,7 +11,6 @@ import {
ResolutionDetails,
StaleEvent,
} from '../src';
import { v4 as uuid } from 'uuid';

const TIMEOUT = 1000;

Expand Down Expand Up @@ -82,7 +82,9 @@ describe('Events', () => {
jest.clearAllMocks();
clientId = uuid();
// hacky, but it's helpful to clear the handlers between tests
/* eslint-disable @typescript-eslint/no-explicit-any */
(OpenFeature as any)._clientEventHandlers = new Map();
/* eslint-disable @typescript-eslint/no-explicit-any */
(OpenFeature as any)._clientEvents = new Map(); });

beforeEach(() => {
Expand Down Expand Up @@ -166,24 +168,23 @@ describe('Events', () => {
const provider = new MockProvider();
const client = OpenFeature.getClient(clientId);

let clientHandlerRan = false;
let apiHandlerRan = false;

client.addHandler(ProviderEvents.Ready, () => {
clientHandlerRan = true;
if (clientHandlerRan && apiHandlerRan) {
done();
}
});

OpenFeature.addHandler(ProviderEvents.Ready, () => {
apiHandlerRan = true;
if (clientHandlerRan && apiHandlerRan) {
done();
}
Promise.all([
new Promise<void>((resolve) => {
client.addHandler(ProviderEvents.Error, () => {
resolve();
});
}),
new Promise<void>((resolve) => {
OpenFeature.addHandler(ProviderEvents.Error, () => {
resolve();
});
})
]).then(() => {
done();
});

OpenFeature.setProvider(clientId, provider);
provider.events?.emit(ProviderEvents.Error);
});
});

Expand Down Expand Up @@ -344,11 +345,13 @@ describe('Events', () => {
});

describe('Requirement 5.2.3,', () => {
it('The event details contain the client name associated with the event in the API', (done) => {
const provider = new MockProvider();
it('The event details MUST contain the provider name associated with the event.', (done) => {
const providerName = '5.2.3';
const provider = new MockProvider({ name: providerName });
const client = OpenFeature.getClient(clientId);

client.addHandler(ProviderEvents.Ready, (details) => {
expect(details?.providerName).toEqual(providerName);
expect(details?.clientName).toEqual(clientId);
done();
});
Expand Down Expand Up @@ -493,20 +496,31 @@ describe('Events', () => {
});

describe('Requirement 5.3.3', () => {
it('`PROVIDER_READY` handlers added after the provider is already in a ready state MUST run immediately.', (done) => {
const provider = new MockProvider();
const client = OpenFeature.getClient(clientId);

OpenFeature.setProvider(clientId, provider);
expect(provider.initialize).toHaveBeenCalled();

let handlerCalled = false;
client.addHandler(ProviderEvents.Ready, () => {
if (!handlerCalled) {
handlerCalled = true;
done();
}
describe('API', () => {
it('Handlers attached after the provider is already in the associated state, MUST run immediately.', (done) => {
const provider = new MockProvider({ initialStatus: ProviderStatus.ERROR });

OpenFeature.setProvider(clientId, provider);
expect(provider.initialize).not.toHaveBeenCalled();

OpenFeature.addHandler(ProviderEvents.Error, () => {
done();
});
});
});

describe('client', () => {
it('Handlers attached after the provider is already in the associated state, MUST run immediately.', (done) => {
const provider = new MockProvider({ initialStatus: ProviderStatus.READY });
const client = OpenFeature.getClient(clientId);

OpenFeature.setProvider(clientId, provider);
expect(provider.initialize).not.toHaveBeenCalled();

client.addHandler(ProviderEvents.Ready, () => {
done();
});
});
});
});
});
4 changes: 2 additions & 2 deletions packages/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<!-- x-hide-in-docs-end -->
<!-- The 'github-badges' class is used in the docs -->
<p align="center" class="github-badges">
<a href="https://github.com/open-feature/spec/tree/v0.6.0">
<img alt="Specification" src="https://img.shields.io/static/v1?label=specification&message=v0.6.0&color=yellow&style=for-the-badge" />
<a href="https://github.com/open-feature/spec/tree/v0.7.0">
<img alt="Specification" src="https://img.shields.io/static/v1?label=specification&message=v0.7.0&color=yellow&style=for-the-badge" />
</a>
<!-- x-release-please-start-version -->
<a href="https://github.com/open-feature/js-sdk/releases/tag/js-sdk-v1.4.2">
Expand Down
22 changes: 11 additions & 11 deletions packages/server/src/client/open-feature-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import {
EventHandler,
FlagValue,
FlagValueType,
Hook,
HookContext,
InternalEventEmitter,
JsonValue,
Logger,
ManageContext,
OpenFeatureError,
InternalEventEmitter,
ProviderEvents,
ProviderStatus,
ResolutionDetails,
StandardResolutionReasons,
SafeLogger,
Hook,
ManageContext,
StandardResolutionReasons,
statusMatchesEvent
} from '@openfeature/shared';
import { FlagEvaluationOptions } from '../evaluation';
import { OpenFeature } from '../open-feature';
import { Client } from './client';
import { Provider } from '../provider';
import { FlagEvaluationOptions } from '../evaluation';
import { Client } from './client';

type OpenFeatureClientOptions = {
name?: string;
Expand Down Expand Up @@ -56,12 +56,12 @@ export class OpenFeatureClient implements Client, ManageContext<OpenFeatureClien

addHandler<T extends ProviderEvents>(eventType: T, handler: EventHandler<T>): void {
this.emitterAccessor().addHandler(eventType, handler);
const providerReady = !this._provider.status || this._provider.status === ProviderStatus.READY;
const shouldRunNow = statusMatchesEvent(eventType, this._provider.status);

if (eventType === ProviderEvents.Ready && providerReady) {
// run immediately, we're ready.
if (shouldRunNow) {
// run immediately, we're in the matching state
try {
handler({ clientName: this.metadata.name });
handler({ clientName: this.metadata.name, providerName: this._provider.metadata.name });
} catch (err) {
this._logger?.error('Error running event handler:', err);
}
Expand Down
7 changes: 3 additions & 4 deletions packages/server/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
JsonArray,
JsonObject,
JsonValue,
ResolutionDetails,
StandardResolutionReasons,
Provider,
OpenFeature,
OpenFeatureClient,
Provider,
ResolutionDetails,
StandardResolutionReasons,
TransactionContext,
TransactionContextPropagator,
} from '../src';
Expand Down Expand Up @@ -348,7 +348,6 @@ describe('OpenFeatureClient', () => {
metadata: {
name: 'error-mock',
},

resolveNumberEvaluation: jest.fn((): Promise<ResolutionDetails<number>> => {
throw new Error(NON_OPEN_FEATURE_ERROR_MESSAGE); // throw a non-open-feature error
}),
Expand Down
Loading