Skip to content

Commit

Permalink
chore: some files split and begining of logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Zork33 committed Oct 26, 2023
2 parents ffa1610 + cd38feb commit 9187879
Show file tree
Hide file tree
Showing 48 changed files with 2,138 additions and 1,856 deletions.
16 changes: 16 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Install ydb-platform/slo-tests

GIT: https://github.com/ydb-platform/slo-tests.git
Start `docker-compose up -d` to have YDB running in local container
Check that YDB has started `http://localhost:8765/monitoring/cluster/tenants`

## Install

set YDB_ANONYMOUS_CREDENTIALS=1
set YDB_SSL_ROOT_CERTIFICATES_FILE=C:\work\slo-tests\playground\data\ydb_certs\cert.pem
set YDB_SDK_LOGLEVEL=debug
npm run test:integration:development

src/__tests__/bytestring-identity.test.ts
src/__tests__/create-table.test.ts

22 changes: 16 additions & 6 deletions src/ydbSdkContext.ts → src/DriverContext.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import {Context, getContext, NOT_A_CONTEXT} from "./utils/context";
import Driver from "./driver";
import {getLoggerFromObject} from "./utils/getLoggerFromObject";
import {Logger} from "./utils/simple-logger";

/**
* Context with reference to the head object - driver.
*/
export class YdbSdkContext extends Context {
export class DriverContext extends Context {

readonly logger: Logger;

private constructor(context: Context, readonly driver: Driver) {
super(context);

this.logger = getLoggerFromObject(this.driver);
}


/**
* 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.
*/
static getSafe(driver: Driver, methodName: string) {
const ctx = getContext();

console.info(2000, ctx)
if (!(driver instanceof Driver)) {
throw Error('Not the Driver! Probably the object does not have such field');
}

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

if (context === NOT_A_CONTEXT) {
context = new YdbSdkContext(ctx, driver);
context = new DriverContext(ctx, driver);
}

context.trace(methodName);
Expand All @@ -35,10 +45,10 @@ export class YdbSdkContext extends Context {
static get(methodName: string) {
const ctx = getContext();

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

if (context === NOT_A_CONTEXT) {
throw new Error('YdbSdkContext is not in the context chain. Consider using YdbSdkContext.getSafe()')
throw new Error('RiverContext is not in the context chain. Consider using RiverContext.getSafe()')
}

context.trace(methodName);
Expand Down
17 changes: 10 additions & 7 deletions src/__tests__/integration/bytestring-identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {destroyDriver, initDriver, TABLE} from '../../test-utils';
import {Column, Session, TableDescription} from '../../table';
import {declareType, TypedData, Types} from '../../types';
import {withRetries} from '../../retries';
import {DriverContext} from "../../DriverContext";

async function createTable(session: Session) {
await session.dropTable(TABLE);
Expand Down Expand Up @@ -77,13 +78,15 @@ describe('bytestring identity', () => {

beforeAll(async () => {
driver = await initDriver();
await driver.tableClient.withSession(async (session) => {
await createTable(session);
await fillTableWithData(session, initialRows);

const {resultSets} = await session.executeQuery(`SELECT * FROM ${TABLE}`);
actualRows = Row.createNativeObjects(resultSets[0]) as Row[];
});
await DriverContext.getSafe(driver, 'test.beforeAll').do(() =>
driver.tableClient.withSession(async (session) => {
await createTable(session);
await fillTableWithData(session, initialRows);

const {resultSets} = await session.executeQuery(`SELECT *
FROM ${TABLE}`);
actualRows = Row.createNativeObjects(resultSets[0]) as Row[];
}));
});

it('Types.TEXT keeps the original string in write-read cycle', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/integration/retries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import {
Undetermined,
YdbError,
} from '../../errors';
import {FallbackLogger} from '../../utils/simple-logger';
import {RetryParameters, retryable} from '../../retries';
import {LogLevel, SimpleLogger} from '../../utils/simple-logger';
import {retryable, RetryParameters} from '../../retries';
import {destroyDriver, initDriver} from '../../test-utils';
import {pessimizable} from '../../utils';

const logger = new FallbackLogger({level: 'error'});
const logger = new SimpleLogger({level: LogLevel.error});
class ErrorThrower {
constructor(public endpoint: Endpoint) {}

Expand Down
2 changes: 0 additions & 2 deletions src/__tests__/unit/AuthErrors.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getContext, Context } from '../../utils/context';
import { SimpleLogger as FallbackLogger} from '../../utils/simple-logger';
import { ServiceError } from '@grpc/grpc-js/build/src/call';
import {IamAuthService, StaticCredentialsAuthService} from '../../credentials';
import { TransportUnavailable } from '../../errors';
Expand Down
54 changes: 54 additions & 0 deletions src/__tests__/unit/driverContext.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {DriverContext} from '../../DriverContext';
import Driver from "./../../driver";
import {buildTestLogger} from "../../utils/tests/test-logger";

describe('driverConext', () => {

it('getSafe', async () => {
const {testLogger, testLoggerFn} = buildTestLogger()

// Note: This way does not go for unit test - since Driver components initiates async start
// const driver = new Driver({
// connectionString: 'http://test.com:1111/?database=test',
// authService: new AnonymousAuthService(),
// logger: testLogger});

// testLoggerFn.mockReset();

const driver = Object.create(Driver.prototype) as Driver;
(driver as any).logger = testLogger;

const ctx1 = DriverContext.getSafe(driver, 'method1');
await ctx1.do(() => {
const ctx2 = DriverContext.getSafe(driver, 'method2');
expect(ctx2).toBe(ctx1);
});
expect(testLoggerFn.mock.calls).toEqual([['trace', 'method1'], ['trace', 'method2']]);
});

it('get - ok', async () => {
const {testLogger, testLoggerFn} = buildTestLogger()

// Note: This way does not go for unit test - since Driver components initiates async start
// const driver = new Driver({
// connectionString: 'http://test.com:1111/?database=test',
// authService: new AnonymousAuthService(),
// logger: testLogger});

// testLoggerFn.mockReset();

const driver = Object.create(Driver.prototype) as Driver;
(driver as any).logger = testLogger;

const ctx1 = DriverContext.getSafe(driver,'method1');
await ctx1.do(() => {
const ctx2 = DriverContext.get('method2');
expect(ctx2).toBe(ctx1);
});
expect(testLoggerFn.mock.calls).toEqual([['trace', 'method1'], ['trace', 'method2']]);
});

it('get - error', async () => {
expect(() => DriverContext.get('method')).toThrow();
});
});
10 changes: 5 additions & 5 deletions src/__tests__/unit/utils/simple-logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SimpleLogger, setMockConsole } from '../../../utils/simple-logger';
import {SimpleLogger, setMockConsole, LogLevel, Logger} from '../../../utils/simple-logger';
import { FakeTimersFixture } from '../../../utils/tests/fake-timers-fixture';
import Mock = jest.Mock;

Expand Down Expand Up @@ -37,7 +37,7 @@ describe('utils.simple-logger', () => {
trace: false,
},
{
level: SimpleLogger.LogLevel.warn,
level: LogLevel.warn,
fatal: true,
error: true,
warn: true,
Expand All @@ -46,7 +46,7 @@ describe('utils.simple-logger', () => {
trace: false,
},
{
level: SimpleLogger.LogLevel.debug,
level: LogLevel.debug,
fatal: true,
error: true,
warn: true,
Expand All @@ -55,7 +55,7 @@ describe('utils.simple-logger', () => {
trace: false,
},
{
level: 'wrong' as SimpleLogger.LogLevel,
level: 'wrong' as LogLevel,
fatal: true,
error: true,
warn: true,
Expand All @@ -67,7 +67,7 @@ describe('utils.simple-logger', () => {

// eslint-disable-next-line @typescript-eslint/no-loop-func
it(`general: ${env}, ${conf.level}`, () => {
let logger: SimpleLogger.Logger;
let logger: Logger;

switch (env) {
case false:
Expand Down
36 changes: 0 additions & 36 deletions src/__tests__/unit/ydbSdkContext.test.ts

This file was deleted.

Loading

0 comments on commit 9187879

Please sign in to comment.