File: test_event_set.cc

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (285 lines) | stat: -rw-r--r-- 8,130 bytes parent folder | download | duplicates (9)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* Copyright (C) Hiroyuki Sato. 2019. ALL RIGHTS RESERVED.
*
* See file LICENSE for terms.
*/

#include <common/test.h>
extern "C" {
#include <ucs/sys/event_set.h>
#include <pthread.h>
#include <sys/epoll.h>
}

#define MAX_BUF_LEN        255

static const char *UCS_EVENT_SET_TEST_STRING  = "ucs_event_set test string";
static const char *UCS_EVENT_SET_EXTRA_STRING = "ucs_event_set extra string";
static const int   UCS_EVENT_SET_EXTRA_NUM    = 0xFF;

enum {
    UCS_EVENT_SET_EXTERNAL_FD = UCS_BIT(0),
};

class test_event_set : public ucs::test_base,
                       public ::testing::TestWithParam<int> {
public:
    static const char *evfd_data;
    static pthread_barrier_t barrier;

    typedef void* (*event_set_pthread_callback_t)(void *arg);

    enum event_set_op_t {
        EVENT_SET_OP_ADD,
        EVENT_SET_OP_MOD,
        EVENT_SET_OP_DEL
    };

    UCS_TEST_BASE_IMPL;

protected:
    void init() {
        if (GetParam() & UCS_EVENT_SET_EXTERNAL_FD) {
            m_ext_fd = epoll_create(1);
            ASSERT_TRUE(m_ext_fd > 0);
        } else {
            m_ext_fd = -1;
        }
    }

    void cleanup() {
        if (GetParam() & UCS_EVENT_SET_EXTERNAL_FD) {
            ASSERT_NE(-1, m_ext_fd);
            close(m_ext_fd);
            m_ext_fd = -1;
        }
    }

    static void* event_set_read_func(void *arg) {
        int *fd = (int *)arg;
        int n;

        n = write(fd[1], evfd_data, strlen(test_event_set::evfd_data));
        if (n == -1) {
            ADD_FAILURE();
        }

        thread_barrier();
        return 0;
    }

    static void* event_set_tmo_func(void *arg) {
        thread_barrier();
        return 0;
    }

    void event_set_init(event_set_pthread_callback_t func) {
        ucs_status_t status;
        int ret;

        if (pipe(m_pipefd) == -1) {
            UCS_TEST_ABORT("pipe() failed with error - " <<
                           strerror(errno));
        }

        ret = pthread_barrier_init(&barrier, NULL, 2);
        if (ret) {
            UCS_TEST_ABORT("pthread_barrier_init() failed with error - " <<
                           strerror(errno));
        }

        ret = pthread_create(&m_tid, NULL, func, (void *)&m_pipefd);
        if (ret) {
            UCS_TEST_ABORT("pthread_create() failed with error - " <<
                           strerror(errno));
        }

        if (GetParam() & UCS_EVENT_SET_EXTERNAL_FD) {
            status = ucs_event_set_create_from_fd(&m_event_set, m_ext_fd);
        } else {
            status = ucs_event_set_create(&m_event_set);
        }
        ASSERT_UCS_OK(status);
        EXPECT_TRUE(m_event_set != NULL);
    }

    void event_set_cleanup() {
        ucs_event_set_cleanup(m_event_set);

        pthread_join(m_tid, NULL);
        pthread_barrier_destroy(&barrier);

        close(m_pipefd[0]);
        close(m_pipefd[1]);
    }

    void event_set_ctl(event_set_op_t op, int fd, ucs_event_set_types_t events) {
        ucs_status_t status = UCS_OK;

        switch (op) {
        case EVENT_SET_OP_ADD:
            status = ucs_event_set_add(m_event_set, fd, events,
                                       (void *)(uintptr_t)fd);
            break;
        case EVENT_SET_OP_MOD:
            status = ucs_event_set_mod(m_event_set, fd, events,
                                       (void *)(uintptr_t)fd);
            break;
        case EVENT_SET_OP_DEL:
            status = ucs_event_set_del(m_event_set, fd);
            break;
        default:
            UCS_TEST_ABORT("unknown event set operation - " << op);
        }

        EXPECT_UCS_OK(status);
    }

    void event_set_wait(unsigned exp_event, int timeout_ms,
                        ucs_event_set_handler_t handler, void *arg) {
        unsigned nread  = ucs_sys_event_set_max_wait_events;
        ucs_status_t status;

        /* Check for events on pipe fd */
        status = ucs_event_set_wait(m_event_set, &nread, 0, handler, arg);
        EXPECT_EQ(exp_event, nread);
        EXPECT_UCS_OK(status);
    }

    static void thread_barrier() {
        int ret = pthread_barrier_wait(&barrier);
        EXPECT_TRUE((ret == 0) || (ret == PTHREAD_BARRIER_SERIAL_THREAD));
    }

    int                  m_pipefd[2];
    int                  m_ext_fd;
    pthread_t            m_tid;
    ucs_sys_event_set_t *m_event_set;
};

const char *test_event_set::evfd_data = UCS_EVENT_SET_TEST_STRING;

pthread_barrier_t test_event_set::barrier;

static void event_set_func1(void *callback_data, ucs_event_set_types_t events,
                            void *arg)
{
    char buf[MAX_BUF_LEN];
    char *extra_str = (char *)((void**)arg)[0];
    int *extra_num = (int *)((void**)arg)[1];
    int n;
    int fd = (int)(uintptr_t)callback_data;
    memset(buf, 0, MAX_BUF_LEN);

    EXPECT_EQ(UCS_EVENT_SET_EVREAD, events);

    n = read(fd, buf, MAX_BUF_LEN - 1);
    if (n == -1) {
        ADD_FAILURE();
        return;
    }
    EXPECT_EQ(0, strncmp(UCS_EVENT_SET_TEST_STRING, buf, n));
    EXPECT_EQ(0, strncmp(UCS_EVENT_SET_EXTRA_STRING, extra_str,n ));
    EXPECT_EQ(UCS_EVENT_SET_EXTRA_NUM, *extra_num);
}

static void event_set_func2(void *callback_data, ucs_event_set_types_t events,
                            void *arg)
{
    EXPECT_EQ(UCS_EVENT_SET_EVWRITE, events);
}

static void event_set_func3(void *callback_data, ucs_event_set_types_t events,
                            void *arg)
{
    ADD_FAILURE();
}

static void event_set_func4(void *callback_data, ucs_event_set_types_t events,
                            void *arg)
{
    EXPECT_EQ(UCS_EVENT_SET_EVREAD, events);
}

UCS_TEST_P(test_event_set, ucs_event_set_read_thread) {
    void *arg[] = { (void*)UCS_EVENT_SET_EXTRA_STRING,
                    (void*)&UCS_EVENT_SET_EXTRA_NUM };

    event_set_init(event_set_read_func);
    event_set_ctl(EVENT_SET_OP_ADD, m_pipefd[0],
                  UCS_EVENT_SET_EVREAD);

    thread_barrier();

    event_set_wait(1u, -1, event_set_func1, arg);

    event_set_ctl(EVENT_SET_OP_DEL, m_pipefd[0], 0);
    event_set_cleanup();
}

UCS_TEST_P(test_event_set, ucs_event_set_write_thread) {
    event_set_init(event_set_read_func);
    event_set_ctl(EVENT_SET_OP_ADD, m_pipefd[1],
                  UCS_EVENT_SET_EVWRITE);

    thread_barrier();

    event_set_wait(1u, -1, event_set_func2, NULL);

    event_set_ctl(EVENT_SET_OP_DEL, m_pipefd[1], 0);
    event_set_cleanup();
}

UCS_TEST_P(test_event_set, ucs_event_set_tmo_thread) {
    event_set_init(event_set_tmo_func);
    event_set_ctl(EVENT_SET_OP_ADD, m_pipefd[0],
                  UCS_EVENT_SET_EVREAD);

    thread_barrier();

    event_set_wait(0u, 0, event_set_func3, NULL);

    event_set_ctl(EVENT_SET_OP_DEL, m_pipefd[0], 0);
    event_set_cleanup();
}

UCS_TEST_P(test_event_set, ucs_event_set_trig_modes) {
    void *arg[] = { (void*)UCS_EVENT_SET_EXTRA_STRING,
                    (void*)&UCS_EVENT_SET_EXTRA_NUM };

    event_set_init(event_set_read_func);
    event_set_ctl(EVENT_SET_OP_ADD, m_pipefd[0],
                  UCS_EVENT_SET_EVREAD);

    thread_barrier();

    /* Test level-triggered mode (default) */
    for (int i = 0; i < 10; i++) {
        event_set_wait(1u, 0, event_set_func4, NULL);
    }

    /* Test edge-triggered mode */
    /* Set edge-triggered mode */
    event_set_ctl(EVENT_SET_OP_MOD, m_pipefd[0],
                  UCS_EVENT_SET_EVREAD | UCS_EVENT_SET_EDGE_TRIGGERED);

    /* Should have only one event to read */
    event_set_wait(1u, 0, event_set_func4, NULL);

    /* Should not read nothing */
    for (int i = 0; i < 10; i++) {
        event_set_wait(0u, 0, event_set_func1, arg);
    }

    /* Call the function below directly to read
     * all outstanding data from pipe fd */
    event_set_func1((void*)(uintptr_t)m_pipefd[0], UCS_EVENT_SET_EVREAD, arg);

    event_set_ctl(EVENT_SET_OP_DEL, m_pipefd[0], 0);
    event_set_cleanup();
}

INSTANTIATE_TEST_SUITE_P(ext_fd, test_event_set,
                        ::testing::Values(static_cast<int>(
                                              UCS_EVENT_SET_EXTERNAL_FD)));
INSTANTIATE_TEST_SUITE_P(int_fd, test_event_set, ::testing::Values(0));