File: db_concurrent_test.c

package info (click to toggle)
cfengine3 3.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,256 kB
  • ctags: 9,613
  • sloc: ansic: 116,129; sh: 12,366; yacc: 1,088; makefile: 1,006; lex: 391; perl: 197; xml: 21; sed: 4
file content (136 lines) | stat: -rw-r--r-- 2,914 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <test.h>

#include <cf3.defs.h>
#include <dbm_api.h>
#include <misc_lib.h>                                          /* xsnprintf */


char CFWORKDIR[CF_BUFSIZE];

void tests_setup(void)
{
    xsnprintf(CFWORKDIR, CF_BUFSIZE, "/tmp/db_test.XXXXXX");
    mkdtemp(CFWORKDIR);
}

void tests_teardown(void)
{
    char cmd[CF_BUFSIZE];
    xsnprintf(cmd, CF_BUFSIZE, "rm -rf '%s'", CFWORKDIR);
    system(cmd);
}

struct arg_struct {
    int base;
};


/*****************************************************************
* launch 5 threads
*   fct(i)
*     one by one insert
*     batch insert
*     one by one update
*     batch update
*     delete one by one
*     batch delete
* 
* join
* check
* 0 - 1999
*   first 100
*   last 1900
*   first 500, if %20, update +1
*   last 1500, if %20, update +1
*   first 500, if %50, delete
*   last 1500, if %50, delete
*   
* 2000- 3999 
* 4000- 5999 
* 6000- 7999 
* 8000- 9999 
*****************************************************************/
static void *fct2(void *arguments)
{
    struct arg_struct *args = (struct arg_struct *)arguments;
    int base = (int)((Seq *)args->base);

    CF_DB *db;
    char key[256];
    char val[256];
    OpenDB(&db, dbid_classes);

    for(int i = base*2000; i<base*2000+100; i++) {
        xsnprintf(key, sizeof(key), "foo%d", i);
        xsnprintf(val, sizeof(val), "bar%d", i);
        WriteDB(db, key, val, strlen(val) + 1);
    }
    for(int i = base*2000; i<base*2000+100; i++) {
        xsnprintf(key, sizeof(key), "foo%d", i);
        xsnprintf(val, sizeof(val), "bar%d", i + 1);

        if ( (i % 2) == 0)
        {
            WriteDB(db, key, val, strlen(val) + 1);
        }
    }
    for(int i = base*2000; i<base*2000+100; i++) {
        if ( (i % 5) == 0)
        {
            xsnprintf(key, sizeof(key), "foo%d", i);
            DeleteDB(db, key);
        }
    }

    xsnprintf(key, sizeof(key), "foo%d", base*2000+90);
    assert_int_equal(HasKeyDB(db, key, strlen(key)+1), false);
    xsnprintf(key, sizeof(key), "foo%d", base*2000+88);
    assert_int_equal(HasKeyDB(db, key, strlen(key)+1), true);
    xsnprintf(key, sizeof(key), "foo%d", base*2000+89);
    assert_int_equal(HasKeyDB(db, key, strlen(key)+1), true); 

    CloseDB(db);
    return NULL;
}

void test_db_concurrent(void)
{
    pthread_t tid[10];
    struct arg_struct args[10];
    int i;
    for (i=0; i < 10; i++)
    {
        args[i].base = i; 
        pthread_create(&tid[i], NULL, (void *) fct2, (void *)&args[i]);
    }
    for (i=0; i < 10; i++)
    {
        pthread_join(tid[i], NULL);
    }
}

int main()
{
    PRINT_TEST_BANNER();
    tests_setup();

    const UnitTest tests[] =
        {
            unit_test(test_db_concurrent),
        };

    PRINT_TEST_BANNER();
    int ret = run_tests(tests);

    tests_teardown();
    return ret;
}

/* STUBS */

void FatalError(ARG_UNUSED char *s, ...)
{
    fail();
    exit(42);
}