[12dcd5f] | 1 | /*
|
---|
| 2 | * Copyright (c) 2017 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[39026d7c] | 29 | #include <async.h>
|
---|
[12dcd5f] | 30 | #include <fibril_synch.h>
|
---|
| 31 | #include <pcut/pcut.h>
|
---|
| 32 |
|
---|
[3f932a7e] | 33 | PCUT_INIT;
|
---|
[12dcd5f] | 34 |
|
---|
| 35 | PCUT_TEST_SUITE(fibril_timer);
|
---|
| 36 |
|
---|
| 37 | static void test_timeout_fn(void *arg)
|
---|
| 38 | {
|
---|
| 39 | int *i;
|
---|
| 40 |
|
---|
| 41 | i = (int *)arg;
|
---|
| 42 | ++*i;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | PCUT_TEST(create_destroy)
|
---|
| 46 | {
|
---|
| 47 | fibril_timer_t *t;
|
---|
| 48 |
|
---|
| 49 | t = fibril_timer_create(NULL);
|
---|
| 50 | PCUT_ASSERT_NOT_NULL(t);
|
---|
| 51 | fibril_timer_destroy(t);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | PCUT_TEST(create_destroy_user_lock)
|
---|
| 55 | {
|
---|
| 56 | fibril_mutex_t lock;
|
---|
| 57 | fibril_timer_t *t;
|
---|
| 58 |
|
---|
| 59 | fibril_mutex_initialize(&lock);
|
---|
| 60 | t = fibril_timer_create(&lock);
|
---|
| 61 | PCUT_ASSERT_NOT_NULL(t);
|
---|
| 62 | fibril_timer_destroy(t);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | PCUT_TEST(set_clear_locked)
|
---|
| 66 | {
|
---|
| 67 | fibril_mutex_t lock;
|
---|
| 68 | fibril_timer_t *t;
|
---|
| 69 | fibril_timer_state_t fts;
|
---|
| 70 | int cnt;
|
---|
| 71 |
|
---|
| 72 | fibril_mutex_initialize(&lock);
|
---|
| 73 | t = fibril_timer_create(&lock);
|
---|
| 74 | PCUT_ASSERT_NOT_NULL(t);
|
---|
| 75 |
|
---|
| 76 | fibril_mutex_lock(&lock);
|
---|
| 77 | cnt = 0;
|
---|
| 78 |
|
---|
| 79 | fibril_timer_set_locked(t, 100 * 1000 * 1000, test_timeout_fn, &cnt);
|
---|
[39026d7c] | 80 | async_usleep(1000);
|
---|
[12dcd5f] | 81 | fts = fibril_timer_clear_locked(t);
|
---|
| 82 | PCUT_ASSERT_INT_EQUALS(fts_active, fts);
|
---|
| 83 |
|
---|
| 84 | PCUT_ASSERT_INT_EQUALS(0, cnt);
|
---|
| 85 | fibril_mutex_unlock(&lock);
|
---|
| 86 |
|
---|
| 87 | fibril_timer_destroy(t);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | PCUT_TEST(set_clear_not_locked)
|
---|
| 91 | {
|
---|
| 92 | fibril_mutex_t lock;
|
---|
| 93 | fibril_timer_t *t;
|
---|
| 94 | fibril_timer_state_t fts;
|
---|
| 95 | int cnt;
|
---|
| 96 |
|
---|
| 97 | fibril_mutex_initialize(&lock);
|
---|
| 98 | t = fibril_timer_create(&lock);
|
---|
| 99 | PCUT_ASSERT_NOT_NULL(t);
|
---|
| 100 |
|
---|
| 101 | cnt = 0;
|
---|
| 102 | fibril_timer_set(t, 100 * 1000 * 1000, test_timeout_fn, &cnt);
|
---|
[39026d7c] | 103 | async_usleep(1000);
|
---|
[12dcd5f] | 104 | fts = fibril_timer_clear(t);
|
---|
| 105 | PCUT_ASSERT_INT_EQUALS(fts_active, fts);
|
---|
| 106 |
|
---|
| 107 | PCUT_ASSERT_INT_EQUALS(0, cnt);
|
---|
| 108 |
|
---|
| 109 | fibril_timer_destroy(t);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | PCUT_TEST(fire)
|
---|
| 113 | {
|
---|
| 114 | fibril_mutex_t lock;
|
---|
| 115 | fibril_timer_t *t;
|
---|
| 116 | fibril_timer_state_t fts;
|
---|
| 117 | int cnt;
|
---|
| 118 |
|
---|
| 119 | fibril_mutex_initialize(&lock);
|
---|
| 120 | t = fibril_timer_create(&lock);
|
---|
| 121 | PCUT_ASSERT_NOT_NULL(t);
|
---|
| 122 |
|
---|
| 123 | fibril_mutex_lock(&lock);
|
---|
| 124 | cnt = 0;
|
---|
| 125 |
|
---|
| 126 | fibril_timer_set_locked(t, 100, test_timeout_fn, &cnt);
|
---|
| 127 | fibril_mutex_unlock(&lock);
|
---|
| 128 |
|
---|
[39026d7c] | 129 | async_usleep(1000);
|
---|
[12dcd5f] | 130 |
|
---|
| 131 | fibril_mutex_lock(&lock);
|
---|
| 132 | fts = fibril_timer_clear_locked(t);
|
---|
| 133 | PCUT_ASSERT_INT_EQUALS(fts_fired, fts);
|
---|
| 134 |
|
---|
| 135 | PCUT_ASSERT_INT_EQUALS(1, cnt);
|
---|
| 136 | fibril_mutex_unlock(&lock);
|
---|
| 137 |
|
---|
| 138 | fibril_timer_destroy(t);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | PCUT_EXPORT(fibril_timer);
|
---|