|
12 | 12 | #include <MicroOcpp/Operations/BootNotification.h> |
13 | 13 | #include <MicroOcpp/Operations/StatusNotification.h> |
14 | 14 | #include <MicroOcpp/Operations/CustomOperation.h> |
| 15 | +#include <MicroOcpp/Model/Boot/BootService.h> |
15 | 16 | #include <MicroOcpp/Model/Transactions/TransactionStore.h> |
16 | 17 | #include <MicroOcpp/Debug.h> |
17 | 18 | #include <catch2/catch.hpp> |
@@ -309,5 +310,121 @@ TEST_CASE( "Boot Behavior" ) { |
309 | 310 |
|
310 | 311 | } |
311 | 312 |
|
| 313 | + SECTION("Auto recovery") { |
| 314 | + |
| 315 | + //start transaction which will persist a few boot cycles, but then will be wiped by auto recovery |
| 316 | + loop(); |
| 317 | + beginTransaction("mIdTag"); |
| 318 | + loop(); |
| 319 | + REQUIRE( getChargePointStatus() == ChargePointStatus_Charging ); |
| 320 | + |
| 321 | + declareConfiguration<const char*>("keepConfigOverRecovery", "originalVal"); |
| 322 | + configuration_save(); |
| 323 | + |
| 324 | + mocpp_deinitialize(); |
| 325 | + |
| 326 | + //MO has 2 unexpected power cycles. Probably just back luck - keep the local state and configuration |
| 327 | + |
| 328 | + //Increase the power cycle counter manually because it's not possible to interrupt the MO lifecycle during unit tests |
| 329 | + BootStats bootstats; |
| 330 | + BootService::loadBootStats(filesystem, bootstats); |
| 331 | + bootstats.bootNr += 2; |
| 332 | + BootService::storeBootStats(filesystem, bootstats); |
| 333 | + |
| 334 | + mocpp_initialize(loopback, ChargerCredentials(), filesystem, /*enable auto recovery*/ true); |
| 335 | + BootService::loadBootStats(filesystem, bootstats); |
| 336 | + REQUIRE( bootstats.getBootFailureCount() == 2 + 1 ); //two boot failures have been measured, +1 because each power cycle is counted as potentially failing until reaching the long runtime barrier |
| 337 | + |
| 338 | + loop(); |
| 339 | + |
| 340 | + REQUIRE( getChargePointStatus() == ChargePointStatus_Charging ); |
| 341 | + |
| 342 | + REQUIRE( !strcmp(declareConfiguration<const char*>("keepConfigOverRecovery", "otherVal")->getString(), "originalVal") ); |
| 343 | + |
| 344 | + //check that the power cycle counter has been updated properly after the controller has been running stable over a long time |
| 345 | + mtime += MO_BOOTSTATS_LONGTIME_MS; |
| 346 | + loop(); |
| 347 | + BootService::loadBootStats(filesystem, bootstats); |
| 348 | + REQUIRE( bootstats.getBootFailureCount() == 0 ); |
| 349 | + |
| 350 | + mocpp_deinitialize(); |
| 351 | + |
| 352 | + //MO has 10 power cycles without running for at least 3 minutes and wipes the local state, but keeps the configuration |
| 353 | + |
| 354 | + BootStats bootstats2; |
| 355 | + BootService::loadBootStats(filesystem, bootstats2); |
| 356 | + bootstats2.bootNr += 10; |
| 357 | + BootService::storeBootStats(filesystem, bootstats2); |
| 358 | + |
| 359 | + mocpp_initialize(loopback, ChargerCredentials(), filesystem, /*enable auto recovery*/ true); |
| 360 | + |
| 361 | + REQUIRE( !strcmp(declareConfiguration<const char*>("keepConfigOverRecovery", "otherVal")->getString(), "originalVal") ); |
| 362 | + BootStats bootstats3; |
| 363 | + BootService::loadBootStats(filesystem, bootstats3); |
| 364 | + REQUIRE( bootstats3.getBootFailureCount() == 0 + 1 ); //failure count is reset, but +1 because each power cycle is counted as potentially failing until reaching the long runtime barrier |
| 365 | + |
| 366 | + loop(); |
| 367 | + REQUIRE( getChargePointStatus() == ChargePointStatus_Available ); |
| 368 | + |
| 369 | + } |
| 370 | + |
| 371 | + SECTION("Migration") { |
| 372 | + |
| 373 | + //migration removes files from previous MO versions which were running on the controller. This includes the |
| 374 | + //transaction cache, but configs are preserved |
| 375 | + |
| 376 | + auto old_opstore = filesystem->open(MO_FILENAME_PREFIX "opstore.jsn", "w"); //the opstore has been removed in MO v1.2.0 |
| 377 | + old_opstore->write("example content", sizeof("example content") - 1); |
| 378 | + old_opstore.reset(); //flushes the file |
| 379 | + |
| 380 | + loop(); |
| 381 | + auto tx = beginTransaction("mIdTag"); //tx store will also be removed |
| 382 | + auto txNr = tx->getTxNr(); //remember this for later usage |
| 383 | + tx.reset(); //reset this smart pointer |
| 384 | + loop(); |
| 385 | + REQUIRE( getChargePointStatus() == ChargePointStatus_Charging ); |
| 386 | + endTransaction(); |
| 387 | + loop(); |
| 388 | + |
| 389 | + REQUIRE( getOcppContext()->getModel().getTransactionStore()->getTransaction(1, txNr) != nullptr ); //tx exists on flash |
| 390 | + |
| 391 | + declareConfiguration<const char*>("keepConfigOverMigration", "originalVal"); //migration keeps configs |
| 392 | + configuration_save(); |
| 393 | + |
| 394 | + mocpp_deinitialize(); |
| 395 | + |
| 396 | + //After a FW update, the tracked version number has changed |
| 397 | + BootStats bootstats; |
| 398 | + BootService::loadBootStats(filesystem, bootstats); |
| 399 | + snprintf(bootstats.microOcppVersion, sizeof(bootstats.microOcppVersion), "oldFwVers"); |
| 400 | + BootService::storeBootStats(filesystem, bootstats); |
| 401 | + |
| 402 | + mocpp_initialize(loopback, ChargerCredentials(), filesystem); //MO migrates here |
| 403 | + |
| 404 | + size_t msize = 0; |
| 405 | + REQUIRE( filesystem->stat(MO_FILENAME_PREFIX "opstore.jsn", &msize) != 0 ); //opstore has been removed |
| 406 | + |
| 407 | + REQUIRE( getOcppContext()->getModel().getTransactionStore()->getTransaction(1, txNr) == nullptr ); //tx history entry has been removed |
| 408 | + |
| 409 | + REQUIRE( !strcmp(declareConfiguration<const char*>("keepConfigOverMigration", "otherVal")->getString(), "originalVal") ); //config has been preserved |
| 410 | + } |
| 411 | + |
| 412 | + SECTION("Clean unused configs") { |
| 413 | + |
| 414 | + declareConfiguration<const char*>("neverDeclaredInsideMO", "originalVal"); //unused configs will be cleared automatically after the controller has been running for a long time |
| 415 | + configuration_save(); |
| 416 | + |
| 417 | + mocpp_deinitialize(); |
| 418 | + |
| 419 | + mocpp_initialize(loopback, ChargerCredentials(), filesystem); //all configs are loaded here, including the test config of this section |
| 420 | + loop(); |
| 421 | + |
| 422 | + //unused configs will be cleared automatically after long time |
| 423 | + mtime += MO_BOOTSTATS_LONGTIME_MS; |
| 424 | + loop(); |
| 425 | + |
| 426 | + REQUIRE( !strcmp(declareConfiguration<const char*>("neverDeclaredInsideMO", "newVal")->getString(), "newVal") ); //config has been removed |
| 427 | + } |
| 428 | + |
312 | 429 | mocpp_deinitialize(); |
313 | 430 | } |
0 commit comments