Skip to content

Commit

Permalink
chore: context
Browse files Browse the repository at this point in the history
  • Loading branch information
Zork33 committed Oct 20, 2023
1 parent 309a716 commit 0a94a3d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
36 changes: 36 additions & 0 deletions src/__tests__/unit/ydbSdkContext.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {YdbSdkContext} from '../../ydbSdkContext';
import Driver from "./../../driver";
import {buildTestLogger} from "../../utils/tests/test-logger";

describe('ydbSdkConext', () => {

it('getSafe', async () => {
const {testLogger, testLoggerFn} = buildTestLogger()
const driver = {
logger: testLogger,
} as Driver;
const ctx1 = YdbSdkContext.getSafe(driver, 'method1');
await ctx1.do(() => {
const ctx2 = YdbSdkContext.getSafe(driver, 'method2');
expect(ctx2).toBe(ctx1);
});
expect(testLoggerFn.mock.calls).toEqual([['trace', 'method1'], ['trace', 'method2']]);
});

it('get - ok', async () => {
const {testLogger, testLoggerFn} = buildTestLogger()
const driver = {
logger: testLogger,
} as Driver;
const ctx1 = YdbSdkContext.getSafe(driver,'method1');
await ctx1.do(() => {
const ctx2 = YdbSdkContext.get('method2');
expect(ctx2).toBe(ctx1);
});
expect(testLoggerFn.mock.calls).toEqual([['trace', 'method1'], ['trace', 'method2']]);
});

it('get - ok', async () => {
expect(() => YdbSdkContext.get('method')).toThrow();
});
});
2 changes: 1 addition & 1 deletion src/utils/tests/test-logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SimpleLogger } from './simple-logger';
import { SimpleLogger } from '../simple-logger';

export const buildTestLogger = () => {
const testLoggerFn = jest.fn();
Expand Down
10 changes: 6 additions & 4 deletions src/utils/ydbSdkConext.ts → src/ydbSdkContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Context, getContext, NOT_A_CONTEXT} from "./context";
import Driver from "../driver";
import {Context, getContext, NOT_A_CONTEXT} from "./utils/context";
import Driver from "./driver";

/**
* Context with reference to the head object - driver.
Expand All @@ -13,9 +13,11 @@ export class YdbSdkContext extends Context {
* This method should be called in methods that can be called by a client code - if this type of context
* does not already exist, it will be created. It is important to have access to Driver object to build new context.
*/
getSafe(driver: Driver, methodName: string) {
static getSafe(driver: Driver, methodName: string) {
const ctx = getContext();

console.info(2000, ctx)

let context = ctx.findContextByClass<YdbSdkContext>(YdbSdkContext);

if (context === NOT_A_CONTEXT) {
Expand All @@ -30,7 +32,7 @@ export class YdbSdkContext extends Context {
/**
* Returns the context of this type. If there is no such context - throws an error.
*/
get(methodName: string) {
static get(methodName: string) {
const ctx = getContext();

let context = ctx.findContextByClass<YdbSdkContext>(YdbSdkContext);
Expand Down

0 comments on commit 0a94a3d

Please sign in to comment.