Skip to content

Commit e7b3c0d

Browse files
authored
Merge pull request #7894 from jarvte/fix_cellular_unittests
Cellular: CellularDevice unittests fix initialization
2 parents c6e18f9 + 5e69888 commit e7b3c0d

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,11 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_network()
118118
ATHandler_stub::ref_count = 0;
119119

120120
CHECK(dev.open_network(&fh1));
121+
AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value;
121122
CHECK(ATHandler_stub::ref_count == 1);
122123

123124
dev.close_network();
124-
CHECK(ATHandler_stub::ref_count == 0);
125+
CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount);
125126
}
126127

127128
void Test_AT_CellularDevice::test_AT_CellularDevice_close_sms()
@@ -132,10 +133,11 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_sms()
132133
ATHandler_stub::ref_count = 0;
133134

134135
CHECK(dev.open_sms(&fh1));
136+
AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value;
135137
CHECK(ATHandler_stub::ref_count == 1);
136138

137139
dev.close_sms();
138-
CHECK(ATHandler_stub::ref_count == 0);
140+
CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount);
139141
}
140142

141143
void Test_AT_CellularDevice::test_AT_CellularDevice_close_power()
@@ -146,10 +148,11 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_power()
146148
ATHandler_stub::ref_count = 0;
147149

148150
CHECK(dev.open_power(&fh1));
151+
AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value;
149152
CHECK(ATHandler_stub::ref_count == 1);
150153

151154
dev.close_power();
152-
CHECK(ATHandler_stub::ref_count == 0);
155+
CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount);
153156
}
154157

155158
void Test_AT_CellularDevice::test_AT_CellularDevice_close_sim()
@@ -161,11 +164,13 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_sim()
161164

162165

163166
CHECK(dev.open_sim(&fh1));
167+
AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value;
168+
164169
dev.close_sms(); // this should not affect to refcount as it's not opened
165170
CHECK(ATHandler_stub::ref_count == 1);
166171

167172
dev.close_sim();
168-
CHECK(ATHandler_stub::ref_count == 0);
173+
CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount);
169174
}
170175

171176
void Test_AT_CellularDevice::test_AT_CellularDevice_close_information()
@@ -174,21 +179,28 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_information()
174179
AT_CellularDevice dev(que);
175180
FileHandle_stub fh1;
176181
ATHandler_stub::int_value = 0;
182+
ATHandler_stub::ref_count = 0;
183+
ATHandler_stub::fh_value = NULL;
184+
AT_CellularBase_stub::handler_value = NULL;
177185

178186
CHECK(dev.open_information(&fh1));
187+
CHECK(ATHandler_stub::ref_count == 1);
179188

180-
ATHandler_stub::fh_value = NULL;
181-
AT_CellularBase_stub::handler_value = NULL;
189+
// at handler is not found as it's NULL (e.g. AT_CellularBase_stub::handler_value is NULL)
182190
dev.close_information();
191+
CHECK(ATHandler_stub::ref_count == 1);
183192

193+
// same filehandle but different at
184194
ATHandler_stub::fh_value = &fh1;
185195
ATHandler at(&fh1, que, 0, ",");
186196
AT_CellularBase_stub::handler_value = &at;
187197

188198
CHECK(dev.open_information(&fh1));
189-
AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value;
199+
// refcount is two it's one when we create athandler and then in open_information it's incremented by one
200+
CHECK(ATHandler_stub::ref_count == 2);
190201

191202
dev.close_information();
203+
CHECK(ATHandler_stub::ref_count == 1);
192204

193205
ATHandler_stub::fh_value = NULL;
194206
}

features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void ATHandler::set_debug(bool debug_on)
6868

6969
ATHandler::~ATHandler()
7070
{
71-
ATHandler_stub::ref_count = -909;
71+
ATHandler_stub::ref_count = kATHandler_destructor_ref_ount;
7272
}
7373

7474
void ATHandler::inc_ref_count()

features/cellular/UNITTESTS/stubs/ATHandler_stub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
static const int kRead_string_table_size = 100;
2727
static const int kRead_int_table_size = 100;
2828
static const int kResp_stop_count_default = 100;
29+
// set reference count to -909 to separate it from zero so we can test that ATHandler is really deleted.
30+
static const int kATHandler_destructor_ref_ount = -909;
2931

3032
namespace ATHandler_stub {
3133
extern nsapi_error_t nsapi_error_value;

0 commit comments

Comments
 (0)