Skip to content

Commit

Permalink
chore: no-floating-promises
Browse files Browse the repository at this point in the history
Signed-off-by: Naseem <[email protected]>
  • Loading branch information
Naseem committed Dec 23, 2020
1 parent d77a8b9 commit 3eb6ef5
Show file tree
Hide file tree
Showing 24 changed files with 791 additions and 799 deletions.
25 changes: 13 additions & 12 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,34 @@ module.exports = {
"header"
],
extends: [
"./node_modules/gts",
"./node_modules/gts",
],
parser: "@typescript-eslint/parser",
parserOptions: {
"project": "./tsconfig.json"
"project": "./tsconfig.json"
},
rules: {
"@typescript-eslint/no-floating-promises": 2,
"@typescript-eslint/no-this-alias": "off",
"eqeqeq": "off",
"prefer-rest-params": "off",
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "memberLike",
"modifiers": ["private", "protected"],
"format": ["camelCase"],
"leadingUnderscore": "require"
}
"error",
{
"selector": "memberLike",
"modifiers": ["private", "protected"],
"format": ["camelCase"],
"leadingUnderscore": "require"
}
],
"@typescript-eslint/no-unused-vars": ["error", {"argsIgnorePattern": "^_", "args": "after-used"}],
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_", "args": "after-used" }],
"@typescript-eslint/no-inferrable-types": ["error", { ignoreProperties: true }],
"arrow-parens": ["error", "as-needed"],
"prettier/prettier": ["error", { "singleQuote": true, "arrowParens": "avoid" }],
"node/no-deprecated-api": ["warn"],
"header/header": [2, "block", [{
pattern: / \* Copyright The OpenTelemetry Authors[\r\n]+ \*[\r\n]+ \* Licensed under the Apache License, Version 2\.0 \(the \"License\"\);[\r\n]+ \* you may not use this file except in compliance with the License\.[\r\n]+ \* You may obtain a copy of the License at[\r\n]+ \*[\r\n]+ \* https:\/\/www\.apache\.org\/licenses\/LICENSE-2\.0[\r\n]+ \*[\r\n]+ \* Unless required by applicable law or agreed to in writing, software[\r\n]+ \* distributed under the License is distributed on an \"AS IS\" BASIS,[\r\n]+ \* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied\.[\r\n]+ \* See the License for the specific language governing permissions and[\r\n]+ \* limitations under the License\./gm,
template:
pattern: / \* Copyright The OpenTelemetry Authors[\r\n]+ \*[\r\n]+ \* Licensed under the Apache License, Version 2\.0 \(the \"License\"\);[\r\n]+ \* you may not use this file except in compliance with the License\.[\r\n]+ \* You may obtain a copy of the License at[\r\n]+ \*[\r\n]+ \* https:\/\/www\.apache\.org\/licenses\/LICENSE-2\.0[\r\n]+ \*[\r\n]+ \* Unless required by applicable law or agreed to in writing, software[\r\n]+ \* distributed under the License is distributed on an \"AS IS\" BASIS,[\r\n]+ \* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied\.[\r\n]+ \* See the License for the specific language governing permissions and[\r\n]+ \* limitations under the License\./gm,
template:
`\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the "License");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an "AS IS" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n `
}]]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,34 +133,34 @@ for (const contextManagerClass of [
});
});

it('async function called from nested "with" sync function should return nested context', done => {
it('async function called from nested "with" sync function should return nested context', async () => {
const scope1 = '1' as any;
const scope2 = '2' as any;

const asyncFuncCalledDownstreamFromSync = async () => {
await (async () => {})();
assert.strictEqual(contextManager.active(), scope2);
return done();
return;
};

contextManager.with(scope1, () => {
await contextManager.with(scope1, async () => {
assert.strictEqual(contextManager.active(), scope1);
contextManager.with(scope2, () =>
await contextManager.with(scope2, () =>
asyncFuncCalledDownstreamFromSync()
);
assert.strictEqual(contextManager.active(), scope1);
});
assert.strictEqual(contextManager.active(), ROOT_CONTEXT);
});

it('should not loose the context', done => {
it('should not loose the context', async () => {
const scope1 = '1' as any;

contextManager.with(scope1, async () => {
await contextManager.with(scope1, async () => {
assert.strictEqual(contextManager.active(), scope1);
await new Promise(resolve => setTimeout(resolve, 100));
assert.strictEqual(contextManager.active(), scope1);
return done();
return;
});
assert.strictEqual(contextManager.active(), ROOT_CONTEXT);
});
Expand Down Expand Up @@ -189,33 +189,38 @@ for (const contextManagerClass of [
assert.strictEqual(contextManager.active(), ROOT_CONTEXT);
});

it('should works with multiple concurrent operations', done => {
it('should works with multiple concurrent operations', async () => {
const scope1 = '1' as any;
const scope2 = '2' as any;
const scope3 = '3' as any;
const scope4 = '4' as any;
let scope4Called = false;

contextManager.with(scope1, async () => {
await contextManager.with(scope1, async () => {
assert.strictEqual(contextManager.active(), scope1);
setTimeout(async () => {
await contextManager.with(scope3, async () => {
assert.strictEqual(contextManager.active(), scope3);
});
assert.strictEqual(contextManager.active(), scope1);
assert.strictEqual(scope4Called, true);
return done();
return;
}, 100);
assert.strictEqual(contextManager.active(), scope1);
assert.strictEqual(await contextManager.active(), scope1);
});
assert.strictEqual(contextManager.active(), ROOT_CONTEXT);
contextManager.with(scope2, async () => {
await contextManager.with(scope2, async () => {
assert.strictEqual(contextManager.active(), scope2);
setTimeout(() => {
contextManager.with(scope4, async () => {
assert.strictEqual(contextManager.active(), scope4);
scope4Called = true;
});
contextManager
.with(scope4, async () => {
assert.strictEqual(contextManager.active(), scope4);
scope4Called = true;
})
.then(
() => {},
_err => {}
);
assert.strictEqual(contextManager.active(), scope2);
}, 20);
assert.strictEqual(contextManager.active(), scope2);
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry-exporter-collector-grpc/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export function onInit<ExportItem, ServiceRequest>(
collector.send(item.objects, item.onSuccess, item.onError);
});
}
});
})
.catch(err => collector.logger.error(err.message));
}

export function send<ExportItem, ServiceRequest>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,53 +63,51 @@ const testCollectorMetricExporter = (params: TestParams) =>
let metrics: metrics.MetricRecord[];
let reqMetadata: grpc.Metadata | undefined;

before(done => {
before(async () => {
server = new grpc.Server();
protoLoader
.load(metricsServiceProtoPath, {
const packageDefinition = await protoLoader.load(
metricsServiceProtoPath,
{
keepCase: false,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs,
})
.then((packageDefinition: protoLoader.PackageDefinition) => {
const packageObject: any = grpc.loadPackageDefinition(
packageDefinition
);
server.addService(
packageObject.opentelemetry.proto.collector.metrics.v1
.MetricsService.service,
{
Export: (data: {
request: collectorTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest;
metadata: grpc.Metadata;
}) => {
try {
exportedData = data.request.resourceMetrics;
reqMetadata = data.metadata;
} catch (e) {
exportedData = undefined;
}
},
}
);

const packageObject: any = grpc.loadPackageDefinition(packageDefinition);
server.addService(
packageObject.opentelemetry.proto.collector.metrics.v1.MetricsService
.service,
{
Export: (data: {
request: collectorTypes.opentelemetryProto.collector.metrics.v1.ExportMetricsServiceRequest;
metadata: grpc.Metadata;
}) => {
try {
exportedData = data.request.resourceMetrics;
reqMetadata = data.metadata;
} catch (e) {
exportedData = undefined;
}
);
const credentials = params.useTLS
? grpc.ServerCredentials.createSsl(
fs.readFileSync('./test/certs/ca.crt'),
[
{
cert_chain: fs.readFileSync('./test/certs/server.crt'),
private_key: fs.readFileSync('./test/certs/server.key'),
},
]
)
: grpc.ServerCredentials.createInsecure();
server.bind(address, credentials);
server.start();
done();
});
},
}
);
const credentials = params.useTLS
? grpc.ServerCredentials.createSsl(
fs.readFileSync('./test/certs/ca.crt'),
[
{
cert_chain: fs.readFileSync('./test/certs/server.crt'),
private_key: fs.readFileSync('./test/certs/server.key'),
},
]
)
: grpc.ServerCredentials.createInsecure();
server.bind(address, credentials);
server.start();
});

after(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,53 +61,47 @@ const testCollectorExporter = (params: TestParams) =>
| undefined;
let reqMetadata: grpc.Metadata | undefined;

before(done => {
before(async () => {
server = new grpc.Server();
protoLoader
.load(traceServiceProtoPath, {
keepCase: false,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs,
})
.then((packageDefinition: protoLoader.PackageDefinition) => {
const packageObject: any = grpc.loadPackageDefinition(
packageDefinition
);
server.addService(
packageObject.opentelemetry.proto.collector.trace.v1.TraceService
.service,
{
Export: (data: {
request: collectorTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest;
metadata: grpc.Metadata;
}) => {
try {
exportedData = data.request.resourceSpans[0];
reqMetadata = data.metadata;
} catch (e) {
exportedData = undefined;
}
},
const packageDefinition = await protoLoader.load(traceServiceProtoPath, {
keepCase: false,
longs: String,
enums: String,
defaults: true,
oneofs: true,
includeDirs,
});
const packageObject: any = grpc.loadPackageDefinition(packageDefinition);
server.addService(
packageObject.opentelemetry.proto.collector.trace.v1.TraceService
.service,
{
Export: (data: {
request: collectorTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceRequest;
metadata: grpc.Metadata;
}) => {
try {
exportedData = data.request.resourceSpans[0];
reqMetadata = data.metadata;
} catch (e) {
exportedData = undefined;
}
);
const credentials = params.useTLS
? grpc.ServerCredentials.createSsl(
fs.readFileSync('./test/certs/ca.crt'),
[
{
cert_chain: fs.readFileSync('./test/certs/server.crt'),
private_key: fs.readFileSync('./test/certs/server.key'),
},
]
)
: grpc.ServerCredentials.createInsecure();
server.bind(address, credentials);
server.start();
done();
});
},
}
);
const credentials = params.useTLS
? grpc.ServerCredentials.createSsl(
fs.readFileSync('./test/certs/ca.crt'),
[
{
cert_chain: fs.readFileSync('./test/certs/server.crt'),
private_key: fs.readFileSync('./test/certs/server.key'),
},
]
)
: grpc.ServerCredentials.createInsecure();
server.bind(address, credentials);
server.start();
});

after(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ export function mockObserver(
return metric;
}

export function mockValueRecorder(): metrics.Metric<metrics.BoundValueRecorder> &
export function mockValueRecorder(): metrics.Metric<
metrics.BoundValueRecorder
> &
api.ValueRecorder {
const name = 'int-recorder';
const metric =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export function mockObserver(
return metric;
}

export function mockValueRecorder(): metrics.Metric<metrics.BoundValueRecorder> &
export function mockValueRecorder(): metrics.Metric<
metrics.BoundValueRecorder
> &
api.ValueRecorder {
const name = 'int-recorder';
const metric =
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ describe('JaegerExporter', () => {
});
});

afterEach(() => {
exporter.shutdown();
afterEach(async () => {
await exporter.shutdown();
});

it('should skip send with empty list', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export class PrometheusExporter implements MetricExporter {
).replace(/^([^/])/, '/$1');

if (config.preventServerStart !== true) {
this.startServer().then(callback);
this.startServer()
.then(callback)
.catch(err => this._logger({ err }, err.message));
} else if (callback) {
callback();
}
Expand Down
Loading

0 comments on commit 3eb6ef5

Please sign in to comment.