From ccb75d2771ffc68fe56b2a5a5d619964caff1fc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teppo=20J=C3=A4rvelin?= Date: Mon, 27 Aug 2018 13:00:20 +0300 Subject: [PATCH 1/2] Cellular: fixed CellularDevice unittests by doing proper initializations between tests. --- .../test_at_cellulardevice.cpp | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp b/features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp index 6bf0beda69..657fc622b4 100644 --- a/features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp +++ b/features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp @@ -118,10 +118,11 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_network() ATHandler_stub::ref_count = 0; CHECK(dev.open_network(&fh1)); + AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; CHECK(ATHandler_stub::ref_count == 1); dev.close_network(); - CHECK(ATHandler_stub::ref_count == 0); + CHECK(ATHandler_stub::ref_count == -909); } void Test_AT_CellularDevice::test_AT_CellularDevice_close_sms() @@ -132,10 +133,11 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_sms() ATHandler_stub::ref_count = 0; CHECK(dev.open_sms(&fh1)); + AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; CHECK(ATHandler_stub::ref_count == 1); dev.close_sms(); - CHECK(ATHandler_stub::ref_count == 0); + CHECK(ATHandler_stub::ref_count == -909); } void Test_AT_CellularDevice::test_AT_CellularDevice_close_power() @@ -146,10 +148,11 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_power() ATHandler_stub::ref_count = 0; CHECK(dev.open_power(&fh1)); + AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; CHECK(ATHandler_stub::ref_count == 1); dev.close_power(); - CHECK(ATHandler_stub::ref_count == 0); + CHECK(ATHandler_stub::ref_count == -909); } void Test_AT_CellularDevice::test_AT_CellularDevice_close_sim() @@ -161,11 +164,13 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_sim() CHECK(dev.open_sim(&fh1)); + AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; + dev.close_sms(); // this should not affect to refcount as it's not opened CHECK(ATHandler_stub::ref_count == 1); dev.close_sim(); - CHECK(ATHandler_stub::ref_count == 0); + CHECK(ATHandler_stub::ref_count == -909); } void Test_AT_CellularDevice::test_AT_CellularDevice_close_information() @@ -174,21 +179,28 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_information() AT_CellularDevice dev(que); FileHandle_stub fh1; ATHandler_stub::int_value = 0; - - CHECK(dev.open_information(&fh1)); - + ATHandler_stub::ref_count = 0; ATHandler_stub::fh_value = NULL; AT_CellularBase_stub::handler_value = NULL; - dev.close_information(); + CHECK(dev.open_information(&fh1)); + CHECK(ATHandler_stub::ref_count == 1); + + // at handler is not found as it's NULL (e.g. AT_CellularBase_stub::handler_value is NULL) + dev.close_information(); + CHECK(ATHandler_stub::ref_count == 1); + + // same filehandle but different at ATHandler_stub::fh_value = &fh1; ATHandler at(&fh1, que, 0, ","); AT_CellularBase_stub::handler_value = &at; CHECK(dev.open_information(&fh1)); - AT_CellularBase_stub::handler_value = AT_CellularBase_stub::handler_at_constructor_value; + // refcount is two it's one when we create athandler and then in open_information it's incremented by one + CHECK(ATHandler_stub::ref_count == 2); dev.close_information(); + CHECK(ATHandler_stub::ref_count == 1); ATHandler_stub::fh_value = NULL; } From 5e6988899cee4dbe42a4f2d9ec7a52b6e0101e61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teppo=20J=C3=A4rvelin?= Date: Tue, 28 Aug 2018 07:36:49 +0300 Subject: [PATCH 2/2] Cellular: review fix removed magic numbers from unit tests. --- .../at/at_cellulardevice/test_at_cellulardevice.cpp | 8 ++++---- features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp | 2 +- features/cellular/UNITTESTS/stubs/ATHandler_stub.h | 2 ++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp b/features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp index 657fc622b4..aabdb717af 100644 --- a/features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp +++ b/features/cellular/UNITTESTS/at/at_cellulardevice/test_at_cellulardevice.cpp @@ -122,7 +122,7 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_network() CHECK(ATHandler_stub::ref_count == 1); dev.close_network(); - CHECK(ATHandler_stub::ref_count == -909); + CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount); } void Test_AT_CellularDevice::test_AT_CellularDevice_close_sms() @@ -137,7 +137,7 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_sms() CHECK(ATHandler_stub::ref_count == 1); dev.close_sms(); - CHECK(ATHandler_stub::ref_count == -909); + CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount); } void Test_AT_CellularDevice::test_AT_CellularDevice_close_power() @@ -152,7 +152,7 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_power() CHECK(ATHandler_stub::ref_count == 1); dev.close_power(); - CHECK(ATHandler_stub::ref_count == -909); + CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount); } void Test_AT_CellularDevice::test_AT_CellularDevice_close_sim() @@ -170,7 +170,7 @@ void Test_AT_CellularDevice::test_AT_CellularDevice_close_sim() CHECK(ATHandler_stub::ref_count == 1); dev.close_sim(); - CHECK(ATHandler_stub::ref_count == -909); + CHECK(ATHandler_stub::ref_count == kATHandler_destructor_ref_ount); } void Test_AT_CellularDevice::test_AT_CellularDevice_close_information() diff --git a/features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp b/features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp index 3daf8da115..81267d0b44 100644 --- a/features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp +++ b/features/cellular/UNITTESTS/stubs/ATHandler_stub.cpp @@ -68,7 +68,7 @@ void ATHandler::set_debug(bool debug_on) ATHandler::~ATHandler() { - ATHandler_stub::ref_count = -909; + ATHandler_stub::ref_count = kATHandler_destructor_ref_ount; } void ATHandler::inc_ref_count() diff --git a/features/cellular/UNITTESTS/stubs/ATHandler_stub.h b/features/cellular/UNITTESTS/stubs/ATHandler_stub.h index 497a4928da..5cb1a772f8 100644 --- a/features/cellular/UNITTESTS/stubs/ATHandler_stub.h +++ b/features/cellular/UNITTESTS/stubs/ATHandler_stub.h @@ -26,6 +26,8 @@ static const int kRead_string_table_size = 100; static const int kRead_int_table_size = 100; static const int kResp_stop_count_default = 100; +// set reference count to -909 to separate it from zero so we can test that ATHandler is really deleted. +static const int kATHandler_destructor_ref_ount = -909; namespace ATHandler_stub { extern nsapi_error_t nsapi_error_value;