efl

Форк
0
/
eet_test_cache.c 
108 строк · 2.6 Кб
1
#ifdef HAVE_CONFIG_H
2
# include "config.h"
3
#endif
4

5
#include <stdlib.h>
6
#ifdef _WIN32
7
# include <io.h> /* close(), unlink() */
8
#else
9
# include <unistd.h> /* close(), unlink() */
10
#endif
11

12
#include <Eina.h>
13
#include <Eet.h>
14

15
#include "eet_suite.h"
16
#include "eet_test_common.h"
17

18
static Eina_Bool open_worker_stop;
19
static Eina_Condition open_worker_cond;
20
static Eina_Lock open_worker_mutex;
21

22
static void *
23
_open_close_worker(void *path, Eina_Thread tid EINA_UNUSED)
24
{
25
   static Eina_Bool first = EINA_TRUE;
26

27
   while (!open_worker_stop)
28
     {
29
        Eet_File *ef = eet_open((char const *)path, EET_FILE_MODE_READ);
30
        if (ef == NULL)
31
          return "eet_open() failed";
32
        else
33
          {
34
             Eet_Error err_code = eet_close(ef);
35
             if (err_code != EET_ERROR_NONE)
36
               return "eet_close() failed";
37
          }
38

39
        if (first)
40
          {
41
             eina_lock_take(&open_worker_mutex);
42
             eina_condition_broadcast(&open_worker_cond);
43
             eina_lock_release(&open_worker_mutex);
44
             first = EINA_FALSE;
45
          }
46
     }
47

48
   return NULL;
49
}
50

51
EFL_START_TEST(eet_test_cache_concurrency)
52
{
53
   const char *buffer = "test data";
54
   Eet_File *ef;
55
   void *thread_ret;
56
   unsigned int n;
57
   Eina_Thread thread;
58
   Eina_Tmpstr *tmpfile = NULL;
59
   Eina_Bool r;
60
   int tmpfd;
61

62
   eina_threads_init();
63

64
   eina_lock_new(&open_worker_mutex);
65
   eina_condition_new(&open_worker_cond, &open_worker_mutex);
66

67
   /* create a file to test with */
68
   /* tmpfile will be created in temporary directory (with eina_environment_tmp) */
69
   tmpfd = eina_file_mkstemp("eet_suite_testXXXXXX", &tmpfile);
70
   fail_if(-1 == tmpfd);
71
   fail_if(!!close(tmpfd));
72
   ef = eet_open(tmpfile, EET_FILE_MODE_WRITE);
73
   fail_if(!ef);
74
   fail_if(!eet_write(ef, "keys/tests", buffer, strlen(buffer) + 1, 0));
75

76
   eina_lock_take(&open_worker_mutex);
77
   /* start a thread that repeatedly opens and closes a file */
78
   open_worker_stop = 0;
79
   r = eina_thread_create(&thread, EINA_THREAD_NORMAL, -1, _open_close_worker, tmpfile);
80
   fail_unless(r);
81

82
   eina_condition_wait(&open_worker_cond);
83
   eina_lock_release(&open_worker_mutex);
84

85
   /* clear the cache repeatedly in this thread */
86
   for (n = 0; n < 20000; ++n)
87
     {
88
        eet_clearcache();
89
     }
90

91
   /* join the other thread, and fail if it returned an error message */
92
   open_worker_stop = 1;
93
   thread_ret = eina_thread_join(thread);
94
   fail_unless(thread_ret == NULL, (char const *)thread_ret);
95

96
   eet_close(ef);
97

98
   fail_if(unlink(tmpfile) != 0);
99

100
   eina_threads_shutdown();
101
   eina_tmpstr_del(tmpfile);
102
}
103
EFL_END_TEST
104

105
void eet_test_cache(TCase *tc)
106
{
107
   tcase_add_test(tc, eet_test_cache_concurrency);
108
}
109

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.