13
13
* limitations under the License
14
14
*/
15
15
16
- const rewire = require ( 'rewire' ) ;
17
16
const sinon = require ( 'sinon' ) ;
17
+ const Counters = require ( '../counters.js' ) ;
18
+ const CountersBase = require ( '../../../autoscaler-common/counters-base.js' ) ;
18
19
19
20
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' ) ;
26
23
27
24
/**
28
25
* @type {import('../../../autoscaler-common/types')
@@ -48,72 +45,57 @@ describe('#scaler-counters', () => {
48
45
} ;
49
46
50
47
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' ) ;
53
50
} ) ;
54
51
55
52
afterEach ( ( ) => {
56
53
sinon . reset ( ) ;
54
+ sinon . restore ( ) ;
57
55
} ) ;
58
56
59
57
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
+ } ) ;
71
65
} ) ;
72
66
73
67
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
+ } ) ;
85
75
} ) ;
86
76
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
+ } ) ;
98
84
} ) ;
99
85
100
86
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
+ } ) ;
112
94
} ) ;
113
95
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 ) ;
115
97
sinon . assert . calledWithExactly (
116
- countersBaseStubs . recordValue ,
98
+ baseRecordValue ,
117
99
'scaler/scaling-duration' ,
118
100
60_000 ,
119
101
{
@@ -126,9 +108,9 @@ describe('#scaler-counters', () => {
126
108
} ) ;
127
109
128
110
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' ) ;
130
112
sinon . assert . calledWithExactly (
131
- countersBaseStubs . recordValue ,
113
+ baseRecordValue ,
132
114
'scaler/scaling-duration' ,
133
115
60_000 ,
134
116
{
0 commit comments