storage_test.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "../minunit.h"
  2. #include <furi.h>
  3. #include <furi_hal_delay.h>
  4. #include <storage/storage.h>
  5. #define STORAGE_LOCKED_FILE "/ext/locked_file.test"
  6. static void storage_file_open_lock_setup() {
  7. Storage* storage = furi_record_open("storage");
  8. File* file = storage_file_alloc(storage);
  9. storage_simply_remove(storage, STORAGE_LOCKED_FILE);
  10. mu_check(storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_WRITE, FSOM_CREATE_NEW));
  11. mu_check(storage_file_write(file, "0123", 4) == 4);
  12. mu_check(storage_file_close(file));
  13. storage_file_free(file);
  14. furi_record_close("storage");
  15. }
  16. static void storage_file_open_lock_teardown() {
  17. Storage* storage = furi_record_open("storage");
  18. mu_check(storage_simply_remove(storage, STORAGE_LOCKED_FILE));
  19. furi_record_close("storage");
  20. }
  21. static int32_t storage_file_locker(void* ctx) {
  22. Storage* storage = furi_record_open("storage");
  23. osSemaphoreId_t semaphore = ctx;
  24. File* file = storage_file_alloc(storage);
  25. furi_check(storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING));
  26. osSemaphoreRelease(semaphore);
  27. furi_hal_delay_ms(1000);
  28. furi_check(storage_file_close(file));
  29. furi_record_close("storage");
  30. storage_file_free(file);
  31. return 0;
  32. }
  33. MU_TEST(storage_file_open_lock) {
  34. Storage* storage = furi_record_open("storage");
  35. bool result = false;
  36. osSemaphoreId_t semaphore = osSemaphoreNew(1, 0, NULL);
  37. File* file = storage_file_alloc(storage);
  38. // file_locker thread start
  39. FuriThread* locker_thread = furi_thread_alloc();
  40. furi_thread_set_name(locker_thread, "StorageFileLocker");
  41. furi_thread_set_stack_size(locker_thread, 2048);
  42. furi_thread_set_context(locker_thread, semaphore);
  43. furi_thread_set_callback(locker_thread, storage_file_locker);
  44. mu_check(furi_thread_start(locker_thread));
  45. // wait for file lock
  46. osSemaphoreAcquire(semaphore, osWaitForever);
  47. osSemaphoreDelete(semaphore);
  48. result = storage_file_open(file, STORAGE_LOCKED_FILE, FSAM_READ_WRITE, FSOM_OPEN_EXISTING);
  49. storage_file_close(file);
  50. // file_locker thread stop
  51. mu_check(furi_thread_join(locker_thread) == osOK);
  52. furi_thread_free(locker_thread);
  53. // clean data
  54. storage_file_free(file);
  55. furi_record_close("storage");
  56. mu_assert(result, "cannot open locked file");
  57. }
  58. MU_TEST_SUITE(storage_file) {
  59. storage_file_open_lock_setup();
  60. MU_RUN_TEST(storage_file_open_lock);
  61. storage_file_open_lock_teardown();
  62. }
  63. int run_minunit_test_storage() {
  64. MU_RUN_SUITE(storage_file);
  65. return MU_EXIT_CODE;
  66. }