-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
esp_event_test.cpp
152 lines (115 loc) · 4.4 KB
/
esp_event_test.cpp
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
/* ESP Event Host-Based Test
This code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#define CATCH_CONFIG_MAIN
#include <stdio.h>
#include "esp_event.h"
#include "catch.hpp"
#include "fixtures.hpp"
extern "C" {
#include "Mocktask.h"
#include "Mockqueue.h"
}
namespace {
const uint32_t QUEUE_SIZE = 32;
static UBaseType_t s_test_priority;
static esp_event_loop_args_t test_event_get_default_loop_args(void)
{
esp_event_loop_args_t loop_config = {
.queue_size = QUEUE_SIZE, // TODO: CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE from esp_system should be used
.task_name = "loop",
.task_priority = s_test_priority,
.task_stack_size = 2048,
.task_core_id = 0
};
return loop_config;
}
void dummy_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { }
}
// TODO: IDF-2693, function definition just to satisfy linker, implement esp_common instead
const char *esp_err_to_name(esp_err_t code) {
return "test";
}
TEST_CASE("create an event loop with any NULL argument fails")
{
MockQueue queue(CreateAnd::IGNORE);
MockMutex sem(CreateAnd::IGNORE);
MockTask task(CreateAnd::IGNORE);
esp_event_loop_handle_t loop; // with dedicated task
esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
CHECK(ESP_ERR_INVALID_ARG == esp_event_loop_create(NULL, &loop));
CHECK(ESP_ERR_INVALID_ARG == esp_event_loop_create(&loop_args, NULL));
}
TEST_CASE("test esp_event_loop_create create_queue_fails(void)")
{
MockQueue queue(CreateAnd::FAIL);
MockMutex sem(CreateAnd::IGNORE);
MockTask task(CreateAnd::IGNORE);
esp_event_loop_handle_t loop;
esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
CHECK(ESP_ERR_NO_MEM == esp_event_loop_create(&loop_args, &loop));
}
TEST_CASE("test esp_event_loop_create create_mutex_fails(void)")
{
MockQueue queue(CreateAnd::IGNORE);
MockMutex sem(CreateAnd::FAIL);
MockTask task(CreateAnd::IGNORE);
esp_event_loop_handle_t loop;
esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
CHECK(ESP_ERR_NO_MEM == esp_event_loop_create(&loop_args, &loop));
}
TEST_CASE("test esp_event_loop_create create_task_fails(void)")
{
MockQueue queue(CreateAnd::IGNORE);
MockMutex sem(CreateAnd::IGNORE);
MockTask task(CreateAnd::FAIL);
esp_event_loop_handle_t loop;
esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
CHECK(ESP_FAIL == esp_event_loop_create(&loop_args, &loop));
}
TEST_CASE("test esp_event_loop_create no_task(void)")
{
MockQueue queue(CreateAnd::IGNORE);
MockMutex sem(CreateAnd::IGNORE);
xQueueTakeMutexRecursive_IgnoreAndReturn(0);
xQueueGiveMutexRecursive_IgnoreAndReturn(0);
xQueueReceive_IgnoreAndReturn(0);
esp_event_loop_handle_t loop = nullptr;
esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
loop_args.task_name = nullptr;
CHECK(ESP_OK == esp_event_loop_create(&loop_args, &loop));
CHECK(loop != nullptr);
CHECK(ESP_OK == esp_event_loop_delete(loop));
xQueueReceive_StopIgnore();
xQueueTakeMutexRecursive_StopIgnore();
xQueueGiveMutexRecursive_StopIgnore();
}
TEST_CASE("test esp_event_loop_create with_task(void)")
{
MockQueue queue(CreateAnd::IGNORE);
MockMutex sem(CreateAnd::IGNORE);
MockTask task(CreateAnd::SUCCEED);
xQueueTakeMutexRecursive_IgnoreAndReturn(0);
xQueueGiveMutexRecursive_IgnoreAndReturn(0);
xQueueReceive_IgnoreAndReturn(0);
esp_event_loop_handle_t loop = nullptr;
esp_event_loop_args_t loop_args = test_event_get_default_loop_args();
loop_args.task_name = "test";
CHECK(ESP_OK == esp_event_loop_create(&loop_args, &loop));
CHECK(loop != nullptr);
CHECK(ESP_OK == esp_event_loop_delete(loop));
xQueueReceive_StopIgnore();
xQueueTakeMutexRecursive_StopIgnore();
xQueueGiveMutexRecursive_StopIgnore();
}
TEST_CASE("registering with ANY_BASE but specific ID fails") {
esp_event_loop_handle_t loop = reinterpret_cast<esp_event_loop_handle_t>(1);
CHECK(esp_event_handler_register_with(loop,
ESP_EVENT_ANY_BASE,
47,
dummy_handler,
nullptr) == ESP_ERR_INVALID_ARG);
}