Skip to content

Commit

Permalink
chore: logger was replaced by SimpleLogger and a test logger was added
Browse files Browse the repository at this point in the history
  • Loading branch information
Zork33 committed Apr 18, 2024
1 parent 279919e commit 9ac6b12
Show file tree
Hide file tree
Showing 45 changed files with 678 additions and 239 deletions.
9 changes: 5 additions & 4 deletions examples/custom-logger/dummy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {main} from '../utils';
import {Logger, LogFn, Driver, getCredentialsFromEnv, setupLogger, setDefaultLogger} from 'ydb-sdk';
import {Logger, LogFn, Driver, getCredentialsFromEnv/*, setupLogger, setDefaultLogger*/} from 'ydb-sdk';

const logFunction: LogFn = (obj: any, ...args: any[]) => {
console.log('Custom logging!', obj, ...args);
Expand All @@ -14,11 +14,12 @@ const MyLogger: Logger = {
};

export async function run(logger: Logger, endpoint: string, database: string) {
setupLogger(MyLogger);
setDefaultLogger(MyLogger); // will work too

// setupLogger(MyLogger);
// setDefaultLogger(MyLogger); // will work too
logger.info('Driver initializing...');
const authService = getCredentialsFromEnv();
const driver = new Driver({endpoint, database, authService});
const driver = new Driver({endpoint, database, authService, logger: MyLogger});
const timeout = 10000;
if (!(await driver.ready(timeout))) {
logger.fatal(`Driver has not become ready in ${timeout}ms!`);
Expand Down
6 changes: 3 additions & 3 deletions examples/custom-logger/pino.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {mainWithoutLogger} from '../utils';
import {Driver, getCredentialsFromEnv, setupLogger} from 'ydb-sdk';
import {Driver, getCredentialsFromEnv/*, setupLogger*/} from 'ydb-sdk';
import pino from 'pino';

export async function run(endpoint: string, database: string) {
const logger = pino({level: 'debug'});
logger.info('Driver initializing...');
setupLogger(logger);
// setupLogger(logger);
const authService = getCredentialsFromEnv();
const driver = new Driver({endpoint, database, authService});
const driver = new Driver({endpoint, database, authService, logger});
const timeout = 10000;
if (!(await driver.ready(timeout))) {
logger.fatal(`Driver has not become ready in ${timeout}ms!`);
Expand Down
8 changes: 4 additions & 4 deletions examples/custom-logger/winston.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {mainWithoutLogger} from '../utils';
import {Driver, getCredentialsFromEnv, Logger, setupLogger} from 'ydb-sdk';
import {Driver, getCredentialsFromEnv, Logger/*, setupLogger*/} from 'ydb-sdk';
import winston from 'winston';

export async function run(endpoint: string, database: string) {
Expand All @@ -14,7 +14,7 @@ export async function run(endpoint: string, database: string) {
trace: 6,
},
}) as unknown as Logger;
/*
/*
Log levels accordance:
YDB-SDK-logger -> winston (npm levels)
fatal -> !!! no such level, set exact as error
Expand All @@ -24,10 +24,10 @@ export async function run(endpoint: string, database: string) {
debug -> debug
trace -> silly(6)
*/
setupLogger(logger);
// setupLogger(logger);
logger.info('Driver initializing...');
const authService = getCredentialsFromEnv();
const driver = new Driver({endpoint, database, authService});
const driver = new Driver({endpoint, database, authService, logger});
const timeout = 10000;
if (!(await driver.ready(timeout))) {
logger.fatal(`Driver has not become ready in ${timeout}ms!`);
Expand Down
7 changes: 3 additions & 4 deletions src/__tests__/e2e/query-service/method-execute.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import DiscoveryService from "../../../discovery/discovery-service";
import {ENDPOINT_DISCOVERY_PERIOD} from "../../../constants";
import {AnonymousAuthService} from "../../../credentials/anonymous-auth-service";
import {getLogger} from "../../../logging";
import {SessionBuilder} from "../../../query/query-session-pool";
import {QuerySession} from "../../../query/query-session";
import {QuerySession, IExecuteResult} from "../../../query";
import {declareType, TypedData, TypedValues, Types} from "../../../types";
import {Ydb} from "ydb-sdk-proto";
import StatsMode = Ydb.Query.StatsMode;
import ExecMode = Ydb.Query.ExecMode;
import {IExecuteResult} from "../../../query/query-session-execute";
import {getDefaultLogger} from "../../../logger/get-default-logger";

const DATABASE = '/local';
const ENDPOINT = 'grpcs://localhost:2136';
Expand Down Expand Up @@ -222,7 +221,7 @@ describe('Query.execute()', () => {
}

async function testOnOneSessionWithoutDriver() {
const logger = getLogger();
const logger = getDefaultLogger();
const authService = new AnonymousAuthService();

discoveryService = new DiscoveryService({
Expand Down
3 changes: 1 addition & 2 deletions src/__tests__/e2e/query-service/query-service-client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Driver from "../../../driver";
import {AnonymousAuthService} from "../../../credentials/anonymous-auth-service";
import {IExecuteResult} from "../../../query/query-session-execute";
import * as errors from "../../../errors";
import path from "path";
import fs from "fs";
import {AUTO_TX} from "../../../table";
import {QuerySession} from "../../../query/query-session";
import {QuerySession, IExecuteResult} from "../../../query";

const DATABASE = '/local';
const ENDPOINT = 'grpcs://localhost:2135';
Expand Down
7 changes: 3 additions & 4 deletions src/__tests__/e2e/query-service/rows-conversion.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import DiscoveryService from "../../../discovery/discovery-service";
import {QuerySession} from "../../../query/query-session";
import {getLogger} from "../../../logging";
import {QuerySession, RowType} from "../../../query";
import {AnonymousAuthService} from "../../../credentials/anonymous-auth-service";
import {ENDPOINT_DISCOVERY_PERIOD} from "../../../constants";
import {SessionBuilder} from "../../../query/query-session-pool";
import {declareType, TypedData, TypedValues, Types} from "../../../types";
import {RowType} from "../../../query/query-session-execute";
import {Ydb} from "ydb-sdk-proto";
import {getDefaultLogger} from "../../../logger/get-default-logger";

const DATABASE = '/local';
const ENDPOINT = 'grpcs://localhost:2136';
Expand Down Expand Up @@ -138,7 +137,7 @@ describe('Rows conversion', () => {
}

async function testOnOneSessionWithoutDriver() {
const logger = getLogger();
const logger = getDefaultLogger();
const authService = new AnonymousAuthService();

discoveryService = new DiscoveryService({
Expand Down
7 changes: 3 additions & 4 deletions src/__tests__/e2e/query-service/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {getLogger} from "../../../logging";
import {AnonymousAuthService} from "../../../credentials/anonymous-auth-service";
import DiscoveryService from "../../../discovery/discovery-service";
import {ENDPOINT_DISCOVERY_PERIOD} from "../../../constants";
import {SessionBuilder} from "../../../query/query-session-pool";
import {QuerySession} from "../../../query/query-session";
import {QuerySession, IExecuteResult} from "../../../query";
import * as symbols from "../../../query/symbols";
import {IExecuteResult} from "../../../query/query-session-execute";
import {getDefaultLogger} from "../../../logger/get-default-logger";

const DATABASE = '/local';
const ENDPOINT = 'grpc://localhost:2136';
Expand Down Expand Up @@ -111,7 +110,7 @@ describe('Query service transactions', () => {
}

async function testOnOneSessionWithoutDriver() {
const logger = getLogger();
const logger = getDefaultLogger();
const authService = new AnonymousAuthService();

discoveryService = new DiscoveryService({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Driver from '../../../driver';
import {declareType, TypedData, Types} from '../../../types';
import {withRetries} from '../../../retries';
import {withRetries} from '../../../retries_obsoleted';
import {Column, TableSession, TableDescription} from "../../../table";
import {initDriver, destroyDriver, TABLE} from "../../../utils/test";

Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/e2e/table-service/retries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import {
Undetermined,
YdbError,
} from '../../../errors';
import {FallbackLogger} from '../../../logging';
import {RetryParameters, retryable} from '../../../retries';
import {retryable, RetryParameters} from '../../../retries_obsoleted';
import {Endpoint} from "../../../discovery";
import {pessimizable} from "../../../utils";
import {initDriver, destroyDriver} from "../../../utils/test";
import {destroyDriver, initDriver} from "../../../utils/test";
import {LogLevel, SimpleLogger} from "../../../logger/simple-logger";

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

Expand Down
31 changes: 20 additions & 11 deletions src/__tests__/unit/auth-errors.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import { FallbackLogger, setupLogger } from '../../logging';
setupLogger(new FallbackLogger({level: 'error'}))

import { ServiceError } from '@grpc/grpc-js/build/src/call';
import { TransportUnavailable } from '../../errors';
import { StatusObject } from '@grpc/grpc-js';
import { Status } from '@grpc/grpc-js/build/src/constants';
import {ServiceError} from '@grpc/grpc-js/build/src/call';
import {TransportUnavailable} from '../../errors';
import {StatusObject} from '@grpc/grpc-js';
import {Status} from '@grpc/grpc-js/build/src/constants';
import {StaticCredentialsAuthService} from "../../credentials/static-credentials-auth-service";
import {IamAuthService} from "../../credentials/iam-auth-service";
import {buildTestLogger} from "../../logger/tests/test-logger";

describe('Retries on errors in auth services', () => {
const mockIamCounter = {retries: 0}
const mockStaticCredCounter = {retries: 0}

// @ts-ignore
let testLogger: Logger;
// @ts-ignore
let testLoggerFn: jest.Mock<any, any>;

function mockCallErrorFromStatus(status: StatusObject): ServiceError {
const message = `${status.code} ${Status[status.code]}: ${status.details}`;
return Object.assign(new Error(message), status);
}

beforeEach(() => {});
beforeAll(() => {
beforeEach(() => {
({testLogger: testLogger, testLoggerFn: testLoggerFn} = buildTestLogger());
});

beforeAll(() => {
jest.mock('ydb-sdk-proto', () => {
const actual = jest.requireActual('ydb-sdk-proto') as typeof import('ydb-sdk-proto')

Expand Down Expand Up @@ -52,7 +58,7 @@ describe('Retries on errors in auth services', () => {
iamEndpoint: '2',
privateKey: Buffer.from('3'),
serviceAccountId: '4',
});
}, testLogger);
// mock jwt request return
iamAuth['getJwtRequest'] = () => '';

Expand All @@ -63,7 +69,10 @@ describe('Retries on errors in auth services', () => {
});

it('Static creds auth service - UNAVAILABLE', async () => {
const staticAuth = new StaticCredentialsAuthService('usr', 'pwd', 'endpoint');
const staticAuth = new StaticCredentialsAuthService(
'usr',
'pwd',
'endpoint', testLogger);

await expect(async () => {
await staticAuth.getAuthMetadata()
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/unit/backoff-settings.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {BackoffSettings} from '../../retries';
import {BackoffSettings} from '../../retries_obsoleted';
import * as utils from '../../utils';
function runTest(backoff: BackoffSettings, retries: number, min: number, max: number) {
it(`have correct value for ${retries} retries`, () => {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/unit/context-do.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Context, setContextIdGenerator} from "../../context/Context";
import {Context, setContextIdGenerator} from "../../context";

describe('Context.do', () => {
beforeEach(() => {
Expand Down
13 changes: 6 additions & 7 deletions src/__tests__/unit/context-ensure.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import {Context} from "../../context/Context";
import {EnsureContext} from "../../context/ensureContext";
import {Context, ensureContext} from "../../context";

describe('ensureContext', () => {
it('positional args', () => {
class Test {
// @ts-ignore
noArgs(): void;
noArgs(ctx: Context): void;
@EnsureContext(true)
@ensureContext(true)
noArgs(ctx: Context): void {
expect(ctx instanceof Context).toBeTruthy();
}

// @ts-ignore
posArgs(n: number, s: string): void;
posArgs(ctx: Context, n: number, s: string): void;
@EnsureContext(true)
@ensureContext(true)
posArgs(ctx: Context, n: number, s: string) {
expect(ctx instanceof Context).toBeTruthy();
expect(n).toBe(12);
Expand All @@ -26,7 +25,7 @@ describe('ensureContext', () => {
static staticNoArgs(): void;
static staticNoArgs(ctx: Context): void;

@EnsureContext(true)
@ensureContext(true)
static staticNoArgs(ctx: Context) {
expect(ctx instanceof Context).toBeTruthy();
}
Expand All @@ -49,15 +48,15 @@ describe('ensureContext', () => {
// noArgs(opts: {
// ctx?: Context,
// }): void;
@EnsureContext()
@ensureContext()
noArgs(opts?: {
ctx?: Context,
}): void {
const ctx = opts!.ctx!;
expect(ctx instanceof Context).toBeTruthy();
}

@EnsureContext(false) // should throw error cause fire arg is not obj
@ensureContext(false) // should throw error cause fire arg is not obj
mismatchTypeOfArgs(n: number, s: string) {
expect(n).toBe(12);
expect(s).toBe('test');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Context} from "./Context";
import {Context} from "./context";

/**
* Decorator that ensures:
Expand All @@ -10,7 +10,7 @@ import {Context} from "./Context";
*
* @param isPositionalArgs
*/
export function EnsureContext(isPositionalArgs?: boolean) { // TODO: Should I got logger somehow?
export function ensureContext(isPositionalArgs?: boolean) { // TODO: Should I got logger somehow?
return (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) => {
const originalMethod = descriptor.value;
// const wrappedMethodName = `${target.constructor.name}::${propertyKey}`; // for regular method
Expand Down
9 changes: 9 additions & 0 deletions src/context/has-object-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Context} from "./context";

export interface HasObjectContext {
/**
* The context in which the object was created. Useful for tracking object initialization and background operations.
* During dispose/destroy operation it is useful to log the current context and the context in which the object was created.
*/
readonly objCtx: Context;
}
4 changes: 4 additions & 0 deletions src/context/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {Context, CtxDispose, CtxCancel, CtxDone, CtxUnsubcribe, CtxIdGenerator, setContextIdGenerator} from './context';
export {ensureContext} from './ensure-context';
export {HasObjectContext} from './has-object-context';
export * as contextSymbols from './symbols';
Loading

0 comments on commit 9ac6b12

Please sign in to comment.