Skip to content

Commit c13a3bf

Browse files
authored
test: privatize forceNewAgent_ (#705)
PR-URL: #705
1 parent 87de955 commit c13a3bf

17 files changed

+72
-60
lines changed

src/index.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {PluginLoaderConfig} from './trace-plugin-loader';
3434
import {pluginLoader} from './trace-plugin-loader';
3535
import {TraceAgent} from './trace-api';
3636
import {traceWriter, TraceWriterConfig} from './trace-writer';
37-
import * as traceUtil from './util';
37+
import {Forceable, FORCE_NEW, packageNameFromPath} from './util';
3838

3939
export {Config, PluginTypes};
4040

@@ -43,7 +43,7 @@ const traceAgent = new TraceAgent('Custom Span API');
4343
const modulesLoadedBeforeTrace: string[] = [];
4444
const traceModuleName = path.join('@google-cloud', 'trace-agent');
4545
for (let i = 0; i < filesLoadedBeforeTrace.length; i++) {
46-
const moduleName = traceUtil.packageNameFromPath(filesLoadedBeforeTrace[i]);
46+
const moduleName = packageNameFromPath(filesLoadedBeforeTrace[i]);
4747
if (moduleName && moduleName !== traceModuleName &&
4848
modulesLoadedBeforeTrace.indexOf(moduleName) === -1) {
4949
modulesLoadedBeforeTrace.push(moduleName);
@@ -53,12 +53,10 @@ for (let i = 0; i < filesLoadedBeforeTrace.length; i++) {
5353
interface TopLevelConfig {
5454
enabled: boolean;
5555
logLevel: number;
56-
forceNewAgent_: boolean;
5756
}
5857

5958
// PluginLoaderConfig extends TraceAgentConfig
60-
type NormalizedConfig = TraceWriterConfig&PluginLoaderConfig&TopLevelConfig&
61-
{forceNewAgent_: boolean};
59+
type NormalizedConfig = TraceWriterConfig&PluginLoaderConfig&TopLevelConfig;
6260

6361
/**
6462
* Normalizes the user-provided configuration object by adding default values
@@ -67,7 +65,8 @@ type NormalizedConfig = TraceWriterConfig&PluginLoaderConfig&TopLevelConfig&
6765
* be modified.
6866
* @return A normalized configuration object.
6967
*/
70-
function initConfig(projectConfig: Config): NormalizedConfig {
68+
function initConfig(projectConfig: Forceable<Config>):
69+
Forceable<NormalizedConfig> {
7170
const envConfig = {
7271
logLevel: Number(process.env.GCLOUD_TRACE_LOGLEVEL) || undefined,
7372
projectId: process.env.GCLOUD_PROJECT,
@@ -89,8 +88,8 @@ function initConfig(projectConfig: Config): NormalizedConfig {
8988
// 3. Environment Variable Set Configuration File (from GCLOUD_TRACE_CONFIG)
9089
// 4. Default Config (as specified in './config')
9190
const config = extend(
92-
true, {forceNewAgent_: false}, defaultConfig, envSetConfig, projectConfig,
93-
envConfig);
91+
true, {[FORCE_NEW]: projectConfig[FORCE_NEW]}, defaultConfig,
92+
envSetConfig, projectConfig, envConfig);
9493

9594
// Enforce the upper limit for the label value size.
9695
if (config.maximumLabelValueSize >
@@ -137,9 +136,9 @@ function stop() {
137136
* trace.start();
138137
*/
139138
export function start(projectConfig?: Config): PluginTypes.TraceAgent {
140-
const config: NormalizedConfig = initConfig(projectConfig || {});
139+
const config = initConfig(projectConfig || {});
141140

142-
if (traceAgent.isActive() && !config.forceNewAgent_) { // already started.
141+
if (traceAgent.isActive() && !config[FORCE_NEW]) { // already started.
143142
throw new Error('Cannot call start on an already started agent.');
144143
} else if (traceAgent.isActive()) {
145144
// For unit tests only.

src/trace-plugin-loader.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ export interface PluginLoaderConfig extends TraceAgentConfig {
4343
plugins: {[pluginName: string]: string};
4444
}
4545

46-
/**
47-
* An interface representing configuration passed to the plugin loader, which
48-
* includes TraceAgent configuration as well.
49-
*/
50-
export interface PluginLoaderSingletonConfig extends PluginLoaderConfig {
51-
forceNewAgent_: boolean;
52-
}
53-
5446
export interface ModulePluginWrapperOptions {
5547
name: string;
5648
path: string;

src/util.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ export interface Constructor<T, Config> {
5050
name: string;
5151
}
5252

53+
export const FORCE_NEW = Symbol('force-new');
54+
55+
export type Forceable<T> = T&{[FORCE_NEW]?: boolean};
56+
5357
/**
5458
* A class that provides access to a singleton.
5559
* We assume that any such singleton is always constructed with two arguments:
@@ -62,8 +66,8 @@ export class Singleton<T, Config> {
6266

6367
constructor(private implementation: Constructor<T, Config>) {}
6468

65-
create(logger: Logger, config: Config&{forceNewAgent_?: boolean}): T {
66-
if (!this[kSingleton] || config.forceNewAgent_) {
69+
create(logger: Logger, config: Forceable<Config>): T {
70+
if (!this[kSingleton] || config[FORCE_NEW]) {
6771
this[kSingleton] = new this.implementation(logger, config);
6872
return this[kSingleton]!;
6973
} else {

test/plugins/test-trace-generic-pool.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
'use strict';
1717

18+
import {FORCE_NEW} from '../../src/util';
19+
1820
var assert = require('assert');
1921
var common = require('./common');
2022
var semver = require('semver');
@@ -30,7 +32,7 @@ describe('generic-pool2', function() {
3032
api = require('../../..').start({
3133
projectId: '0',
3234
samplingRate: 0,
33-
forceNewAgent_: true
35+
[FORCE_NEW]: true
3436
});
3537
genericPool = require('./fixtures/generic-pool2');
3638
});
@@ -89,7 +91,7 @@ describe('generic-pool3', function() {
8991
agent = require('../../..').start({
9092
projectId: '0',
9193
samplingRate: 0,
92-
forceNewAgent_: true
94+
[FORCE_NEW]: true
9395
});
9496
genericPool = require('./fixtures/generic-pool3');
9597
});

test/plugins/test-trace-grpc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as util from '../../src/util';
2323
import * as assert from 'assert';
2424
import { asBaseSpanData } from '../utils';
2525
import { SpanData } from '../../src/plugin-types';
26+
import { FORCE_NEW } from '../../src/util';
2627

2728
var shimmer = require('shimmer');
2829
var common = require('./common'/*.js*/);
@@ -285,7 +286,7 @@ Object.keys(versions).forEach(function(version) {
285286
projectId: '0',
286287
samplingRate: 0,
287288
enhancedDatabaseReporting: true,
288-
forceNewAgent_: true
289+
[FORCE_NEW]: true
289290
});
290291

291292
grpc = require(versions[version]);

test/test-config-credentials.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as nock from 'nock';
1919
import {disableNetConnect, enableNetConnect} from 'nock';
2020
import * as path from 'path';
2121

22+
import {FORCE_NEW} from '../src/util';
2223
import {oauth2, patchTraces} from './nocks';
2324
import * as trace from './trace';
2425
import {plan} from './utils';
@@ -64,7 +65,7 @@ describe('Credentials Configuration', () => {
6465
const config = {
6566
bufferSize: 2,
6667
keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'),
67-
forceNewAgent_: true
68+
[FORCE_NEW]: true
6869
};
6970
const agent = trace.start(config);
7071
const scope = oauth2<TestCredentials>((body) => {
@@ -87,7 +88,7 @@ describe('Credentials Configuration', () => {
8788
const progress = plan(done, 2);
8889
const credentials: TestCredentials =
8990
require('./fixtures/gcloud-credentials.json');
90-
const config = {bufferSize: 2, credentials, forceNewAgent_: true};
91+
const config = {bufferSize: 2, credentials, [FORCE_NEW]: true};
9192
const agent = trace.start(config);
9293
const scope = oauth2<TestCredentials>((body) => {
9394
assert.strictEqual(body.client_id, credentials.client_id);
@@ -117,7 +118,7 @@ describe('Credentials Configuration', () => {
117118
bufferSize: 2,
118119
credentials: correctCredentials,
119120
keyFilename: path.join('test', 'fixtures', 'gcloud-credentials.json'),
120-
forceNewAgent_: true
121+
[FORCE_NEW]: true
121122
};
122123
const agent = trace.start(config);
123124
const scope = oauth2<TestCredentials>((body) => {

test/test-config-max-label-size.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818

1919
import { Constants } from '../src/constants';
2020
import { traceWriter } from '../src/trace-writer';
21+
import { FORCE_NEW } from '../src/util';
2122

2223
var assert = require('assert');
2324
var trace = require('../..');
2425

2526
describe('maximumLabelValueSize configuration', function() {
2627
it('should not allow values above server maximum', function() {
27-
trace.start({forceNewAgent_: true, maximumLabelValueSize: 1000000});
28+
trace.start({[FORCE_NEW]: true, maximumLabelValueSize: 1000000});
2829
var valueMax = traceWriter.get().getConfig().maximumLabelValueSize;
2930
assert.strictEqual(valueMax, Constants.TRACE_SERVICE_LABEL_VALUE_LIMIT);
3031
});
3132

3233
it('should not modify values below server maximum', function() {
33-
trace.start({forceNewAgent_: true, maximumLabelValueSize: 10});
34+
trace.start({[FORCE_NEW]: true, maximumLabelValueSize: 10});
3435
var valueMax = traceWriter.get().getConfig().maximumLabelValueSize;
3536
assert.strictEqual(valueMax, 10);
3637
});

test/test-env-log-level.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
'use strict';
1818

19+
import {FORCE_NEW} from '../src/util';
20+
1921
var assert = require('assert');
2022
var gcloudCommon = require('@google-cloud/common');
2123
var shimmer = require('shimmer');
@@ -47,21 +49,21 @@ describe('should respect environment variables', function() {
4749
});
4850

4951
it('should respect GCLOUD_TRACE_LOGLEVEL', function() {
50-
trace.start({forceNewAgent_: true});
52+
trace.start({[FORCE_NEW]: true});
5153
assert.strictEqual(logLevel, gcloudCommon.logger.LEVELS[4]);
5254
});
5355

5456
it('should prefer env to config', function() {
55-
trace.start({logLevel: 2, forceNewAgent_: true});
57+
trace.start({logLevel: 2, [FORCE_NEW]: true});
5658
assert.strictEqual(logLevel, gcloudCommon.logger.LEVELS[4]);
5759
});
5860

5961
it('should fix out of bounds log level', function() {
6062
process.env.GCLOUD_TRACE_LOGLEVEL = '-5';
61-
trace.start({forceNewAgent_: true});
63+
trace.start({[FORCE_NEW]: true});
6264
assert.strictEqual(logLevel, gcloudCommon.logger.LEVELS[0]);
6365
process.env.GCLOUD_TRACE_LOGLEVEL = '300';
64-
trace.start({forceNewAgent_: true});
66+
trace.start({[FORCE_NEW]: true});
6567
assert.strictEqual(logLevel, gcloudCommon.logger.LEVELS[5]);
6668
});
6769
});

test/test-env-project-id.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
'use strict';
1818

19+
import {FORCE_NEW} from '../src/util';
20+
1921
process.env.GCLOUD_PROJECT = '1729';
2022

2123
var trace = require('../..');
@@ -24,12 +26,12 @@ var assert = require('assert');
2426

2527
describe('should respect environment variables', function() {
2628
it('should respect GCLOUD_PROJECT', function() {
27-
var agent = trace.start({forceNewAgent_: true});
29+
var agent = trace.start({[FORCE_NEW]: true});
2830
assert.equal(agent.config.projectId, 1729);
2931
});
3032

3133
it('should prefer env to config', function() {
32-
var agent = trace.start({projectId: 1927, forceNewAgent_: true});
34+
var agent = trace.start({projectId: 1927, [FORCE_NEW]: true});
3335
assert.equal(agent.config.projectId, 1729);
3436
});
3537
});

test/test-index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import './override-gcp-metadata';
2020
import { TraceAgent } from '../src/trace-api';
2121
import { SpanDataType } from '../src/constants';
22+
import { FORCE_NEW } from '../src/util';
2223

2324
var assert = require('assert');
2425
var nock = require('nock');
@@ -49,7 +50,7 @@ describe('index.js', function() {
4950
describe('in valid environment', function() {
5051
var agent;
5152
before(function() {
52-
agent = trace.start({ projectId: '0', forceNewAgent_: true });
53+
agent = trace.start({ projectId: '0', [FORCE_NEW]: true });
5354
});
5455

5556
it('should get the agent with `Trace.get`', function() {
@@ -74,7 +75,7 @@ describe('index.js', function() {
7475
.get('/computeMetadata/v1/project/project-id')
7576
.times(1)
7677
.reply(404, 'foo');
77-
var agent = trace.start({logLevel: 0, forceNewAgent_: true});
78+
var agent = trace.start({logLevel: 0, [FORCE_NEW]: true});
7879
setTimeout(function() {
7980
assert.ok(!agent.isActive());
8081
scope.done();
@@ -91,7 +92,7 @@ describe('index.js', function() {
9192
nocks.hostname(function() { return 'host1'; });
9293
nocks.instanceId(function() { return 'instance1'; });
9394

94-
var agent = trace.start({logLevel: 0, forceNewAgent_: true});
95+
var agent = trace.start({logLevel: 0, [FORCE_NEW]: true});
9596

9697
setTimeout(function() {
9798
assert.strictEqual(agent.getWriterProjectId(), 'project1');

0 commit comments

Comments
 (0)