Skip to content

Commit 99e4dc3

Browse files
authored
fix(tests): remove use of rewire from counters_test.js (#4)
Rewire causes issues in node v22
1 parent 0b4d8b9 commit 99e4dc3

File tree

1 file changed

+39
-57
lines changed

1 file changed

+39
-57
lines changed

src/scaler/scaler-core/test/counters.test.js

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313
* limitations under the License
1414
*/
1515

16-
const rewire = require('rewire');
1716
const sinon = require('sinon');
17+
const Counters = require('../counters.js');
18+
const CountersBase = require('../../../autoscaler-common/counters-base.js');
1819

1920
describe('#scaler-counters', () => {
20-
const counters = rewire('../counters.js');
21-
22-
const countersBaseStubs = {
23-
incCounter: sinon.stub(),
24-
recordValue: sinon.stub(),
25-
};
21+
let baseIncCounter = sinon.stub(CountersBase, 'incCounter');
22+
let baseRecordValue = sinon.stub(CountersBase, 'recordValue');
2623

2724
/**
2825
* @type {import('../../../autoscaler-common/types')
@@ -48,72 +45,57 @@ describe('#scaler-counters', () => {
4845
};
4946

5047
beforeEach(() => {
51-
Object.values(countersBaseStubs).forEach((stub) => stub.reset());
52-
counters.__set__('CountersBase', countersBaseStubs);
48+
baseIncCounter = sinon.stub(CountersBase, 'incCounter');
49+
baseRecordValue = sinon.stub(CountersBase, 'recordValue');
5350
});
5451

5552
afterEach(() => {
5653
sinon.reset();
54+
sinon.restore();
5755
});
5856

5957
it('incScalingSuccessCounter uses cluster config to determine counter attributes', async () => {
60-
await counters.incScalingSuccessCounter(cluster, 10);
61-
sinon.assert.calledWithExactly(
62-
countersBaseStubs.incCounter,
63-
'scaler/scaling-success',
64-
{
65-
memorystore_cluster_project_id: 'myProject',
66-
memorystore_cluster_instance_id: 'myCluster',
67-
scaling_method: 'STEPWISE',
68-
scaling_direction: 'SCALE_UP',
69-
},
70-
);
58+
await Counters.incScalingSuccessCounter(cluster, 10);
59+
sinon.assert.calledWithExactly(baseIncCounter, 'scaler/scaling-success', {
60+
memorystore_cluster_project_id: 'myProject',
61+
memorystore_cluster_instance_id: 'myCluster',
62+
scaling_method: 'STEPWISE',
63+
scaling_direction: 'SCALE_UP',
64+
});
7165
});
7266

7367
it('incScalingSuccessCounter overrides cluster config with parameters', async () => {
74-
await counters.incScalingSuccessCounter(cluster, 10, 20, 'DIRECT');
75-
sinon.assert.calledWithExactly(
76-
countersBaseStubs.incCounter,
77-
'scaler/scaling-success',
78-
{
79-
memorystore_cluster_project_id: 'myProject',
80-
memorystore_cluster_instance_id: 'myCluster',
81-
scaling_method: 'DIRECT',
82-
scaling_direction: 'SCALE_DOWN',
83-
},
84-
);
68+
await Counters.incScalingSuccessCounter(cluster, 10, 20, 'DIRECT');
69+
sinon.assert.calledWithExactly(baseIncCounter, 'scaler/scaling-success', {
70+
memorystore_cluster_project_id: 'myProject',
71+
memorystore_cluster_instance_id: 'myCluster',
72+
scaling_method: 'DIRECT',
73+
scaling_direction: 'SCALE_DOWN',
74+
});
8575
});
8676
it('incScalingFailedCounter uses cluster config to determine counter attributes', async () => {
87-
await counters.incScalingFailedCounter(cluster, 10);
88-
sinon.assert.calledWithExactly(
89-
countersBaseStubs.incCounter,
90-
'scaler/scaling-failed',
91-
{
92-
memorystore_cluster_project_id: 'myProject',
93-
memorystore_cluster_instance_id: 'myCluster',
94-
scaling_method: 'STEPWISE',
95-
scaling_direction: 'SCALE_UP',
96-
},
97-
);
77+
await Counters.incScalingFailedCounter(cluster, 10);
78+
sinon.assert.calledWithExactly(baseIncCounter, 'scaler/scaling-failed', {
79+
memorystore_cluster_project_id: 'myProject',
80+
memorystore_cluster_instance_id: 'myCluster',
81+
scaling_method: 'STEPWISE',
82+
scaling_direction: 'SCALE_UP',
83+
});
9884
});
9985

10086
it('incScalingFailedCounter overrides cluster config with parameters', async () => {
101-
await counters.incScalingFailedCounter(cluster, 10, 20, 'DIRECT');
102-
sinon.assert.calledWithExactly(
103-
countersBaseStubs.incCounter,
104-
'scaler/scaling-failed',
105-
{
106-
memorystore_cluster_project_id: 'myProject',
107-
memorystore_cluster_instance_id: 'myCluster',
108-
scaling_method: 'DIRECT',
109-
scaling_direction: 'SCALE_DOWN',
110-
},
111-
);
87+
await Counters.incScalingFailedCounter(cluster, 10, 20, 'DIRECT');
88+
sinon.assert.calledWithExactly(baseIncCounter, 'scaler/scaling-failed', {
89+
memorystore_cluster_project_id: 'myProject',
90+
memorystore_cluster_instance_id: 'myCluster',
91+
scaling_method: 'DIRECT',
92+
scaling_direction: 'SCALE_DOWN',
93+
});
11294
});
11395
it('recordScalingDuration uses cluster config to determine counter attributes', async () => {
114-
await counters.recordScalingDuration(60_000, cluster, 10);
96+
await Counters.recordScalingDuration(60_000, cluster, 10);
11597
sinon.assert.calledWithExactly(
116-
countersBaseStubs.recordValue,
98+
baseRecordValue,
11799
'scaler/scaling-duration',
118100
60_000,
119101
{
@@ -126,9 +108,9 @@ describe('#scaler-counters', () => {
126108
});
127109

128110
it('recordScalingDuration overrides cluster config with parameters', async () => {
129-
await counters.recordScalingDuration(60_000, cluster, 10, 20, 'DIRECT');
111+
await Counters.recordScalingDuration(60_000, cluster, 10, 20, 'DIRECT');
130112
sinon.assert.calledWithExactly(
131-
countersBaseStubs.recordValue,
113+
baseRecordValue,
132114
'scaler/scaling-duration',
133115
60_000,
134116
{

0 commit comments

Comments
 (0)