Skip to content

Commit 7b7dfc6

Browse files
author
Cruz Monrreal
authored
Merge pull request #7216 from mikaleppanen/gt_async_dns
Asynchronous DNS greentea tests
2 parents 567689c + ef14f54 commit 7b7dfc6

12 files changed

+840
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "unity.h"
21+
#include "utest.h"
22+
#include "dns_tests.h"
23+
24+
using namespace utest::v1;
25+
26+
namespace {
27+
int result_ok;
28+
int result_no_mem;
29+
int result_dns_failure;
30+
int result_exp_timeout;
31+
}
32+
33+
void ASYNCHRONOUS_DNS()
34+
{
35+
do_asynchronous_gethostbyname(dns_test_hosts, 1, &result_ok, &result_no_mem, &result_dns_failure, &result_exp_timeout);
36+
37+
TEST_ASSERT(result_ok == 1);
38+
TEST_ASSERT(result_no_mem == 0);
39+
TEST_ASSERT(result_dns_failure == 0);
40+
TEST_ASSERT(result_exp_timeout == 0);
41+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "unity.h"
21+
#include "utest.h"
22+
#include "dns_tests.h"
23+
24+
using namespace utest::v1;
25+
26+
namespace {
27+
int ticker_us = 0;
28+
}
29+
30+
static void test_dns_query_ticker(void)
31+
{
32+
ticker_us += 100;
33+
}
34+
35+
void ASYNCHRONOUS_DNS_CACHE()
36+
{
37+
rtos::Semaphore semaphore;
38+
dns_application_data data;
39+
data.semaphore = &semaphore;
40+
41+
Ticker ticker;
42+
ticker.attach_us(&test_dns_query_ticker, 100);
43+
44+
for (unsigned int i = 0; i < 5; i++) {
45+
int started_us = ticker_us;
46+
47+
nsapi_error_t err = get_interface()->gethostbyname_async(dns_test_hosts[0],
48+
mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data));
49+
TEST_ASSERT(err >= 0);
50+
51+
semaphore.wait();
52+
53+
TEST_ASSERT(data.result == NSAPI_ERROR_OK);
54+
TEST_ASSERT(strlen(data.addr.get_ip_address()) > 1);
55+
56+
int delay_ms = (ticker_us - started_us) / 1000;
57+
58+
static int delay_first = delay_ms / 2;
59+
// Check that cached accesses are at least twice as fast as the first one
60+
TEST_ASSERT_FALSE(i != 0 && delay_ms > delay_first);
61+
62+
printf("DNS: query \"%s\" => \"%s\", time %i ms\n",
63+
dns_test_hosts[0], data.addr.get_ip_address(), delay_ms);
64+
}
65+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "unity.h"
21+
#include "utest.h"
22+
#include "dns_tests.h"
23+
24+
using namespace utest::v1;
25+
26+
void ASYNCHRONOUS_DNS_CANCEL()
27+
{
28+
rtos::Semaphore semaphore;
29+
dns_application_data *data = new dns_application_data[MBED_CONF_APP_DNS_TEST_HOSTS_NUM];
30+
31+
int count = 0;
32+
33+
for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
34+
data[i].value_set = false;
35+
data[i].semaphore = &semaphore;
36+
data[i].req_result = get_interface()->gethostbyname_async(dns_test_hosts[i],
37+
mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data[i]));
38+
TEST_ASSERT(data[i].req_result >= 0 || data[i].req_result == NSAPI_ERROR_NO_MEMORY);
39+
40+
if (data[i].req_result >= 0) {
41+
// Callback will be called
42+
count++;
43+
} else {
44+
// No memory to initiate DNS query, callback will not be called
45+
data[i].result = NSAPI_ERROR_NO_MEMORY;
46+
data[i].value_set = true;
47+
}
48+
}
49+
50+
for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
51+
if (data[i].req_result > 0) {
52+
if (get_interface()->gethostbyname_async_cancel(data[i].req_result) == NSAPI_ERROR_OK) {
53+
count--;
54+
}
55+
}
56+
}
57+
58+
// Wait for callback(s) to complete
59+
for (int i = 0; i < count; i++) {
60+
semaphore.wait();
61+
}
62+
63+
for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
64+
if (!data[i].value_set) {
65+
printf("DNS: query \"%s\" => cancel\n", dns_test_hosts[i]);
66+
continue;
67+
}
68+
TEST_ASSERT(data[i].result == NSAPI_ERROR_OK || data[i].result == NSAPI_ERROR_NO_MEMORY || data[i].result == NSAPI_ERROR_DNS_FAILURE || data[i].result == NSAPI_ERROR_TIMEOUT);
69+
if (data[i].result == NSAPI_ERROR_OK) {
70+
printf("DNS: query \"%s\" => \"%s\"\n",
71+
dns_test_hosts[i], data[i].addr.get_ip_address());
72+
} else if (data[i].result == NSAPI_ERROR_DNS_FAILURE) {
73+
printf("DNS: query \"%s\" => DNS failure\n", dns_test_hosts[i]);
74+
} else if (data[i].result == NSAPI_ERROR_TIMEOUT) {
75+
printf("DNS: query \"%s\" => timeout\n", dns_test_hosts[i]);
76+
} else if (data[i].result == NSAPI_ERROR_NO_MEMORY) {
77+
printf("DNS: query \"%s\" => no memory\n", dns_test_hosts[i]);
78+
}
79+
}
80+
81+
delete[] data;
82+
83+
wait(5.0);
84+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "unity.h"
21+
#include "utest.h"
22+
#include "dns_tests.h"
23+
24+
#include "nsapi_dns.h"
25+
26+
using namespace utest::v1;
27+
28+
namespace {
29+
int result_ok;
30+
int result_no_mem;
31+
int result_dns_failure;
32+
int result_exp_timeout;
33+
34+
const int EXTERNAL_THREAD_SIZE = 2048;
35+
const int EVENT_QUEUE_SIZE = 10;
36+
37+
events::EventQueue *event_queue;
38+
}
39+
40+
static nsapi_error_t event_queue_call(int delay, mbed::Callback<void()> func)
41+
{
42+
if (delay) {
43+
if (event_queue->call_in(delay, func) == 0) {
44+
return NSAPI_ERROR_NO_MEMORY;
45+
}
46+
} else {
47+
if (event_queue->call(func) == 0) {
48+
return NSAPI_ERROR_NO_MEMORY;
49+
}
50+
}
51+
return NSAPI_ERROR_OK;
52+
}
53+
54+
void ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE()
55+
{
56+
// Ensures that cache does not contain entries
57+
do_asynchronous_gethostbyname(dns_test_hosts, MBED_CONF_NSAPI_DNS_CACHE_SIZE, &result_ok, &result_no_mem,
58+
&result_dns_failure, &result_exp_timeout);
59+
60+
TEST_ASSERT(result_ok == MBED_CONF_NSAPI_DNS_CACHE_SIZE);
61+
TEST_ASSERT(result_no_mem == 0);
62+
TEST_ASSERT(result_dns_failure == 0);
63+
TEST_ASSERT(result_exp_timeout == 0);
64+
65+
// Dispatch event queue
66+
Thread eventThread(osPriorityNormal, EXTERNAL_THREAD_SIZE);
67+
EventQueue queue(EVENT_QUEUE_SIZE * EVENTS_EVENT_SIZE);
68+
eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
69+
event_queue = &queue;
70+
71+
nsapi_dns_call_in_set(event_queue_call);
72+
73+
do_asynchronous_gethostbyname(dns_test_hosts_second, MBED_CONF_APP_DNS_SIMULT_QUERIES + 1, &result_ok, &result_no_mem,
74+
&result_dns_failure, &result_exp_timeout);
75+
76+
TEST_ASSERT(result_ok == MBED_CONF_APP_DNS_SIMULT_QUERIES);
77+
TEST_ASSERT(result_no_mem == 1); // last query fails for no memory as expected
78+
TEST_ASSERT(result_dns_failure == 0);
79+
TEST_ASSERT(result_exp_timeout == 0);
80+
81+
// Give event queue time to finalise before destructors
82+
wait(2.0);
83+
84+
nsapi_dns_call_in_set(0);
85+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "unity.h"
21+
#include "utest.h"
22+
#include "dns_tests.h"
23+
24+
using namespace utest::v1;
25+
26+
namespace {
27+
int result_ok;
28+
int result_no_mem;
29+
int result_dns_failure;
30+
int result_exp_timeout;
31+
}
32+
33+
void ASYNCHRONOUS_DNS_INVALID_HOST()
34+
{
35+
// Ensures that cache does not contain entries
36+
do_asynchronous_gethostbyname(dns_test_hosts_second, MBED_CONF_NSAPI_DNS_CACHE_SIZE, &result_ok, &result_no_mem,
37+
&result_dns_failure, &result_exp_timeout);
38+
39+
char dns_test_hosts_new[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN];
40+
memcpy(dns_test_hosts_new, dns_test_hosts, sizeof(dns_test_hosts_new));
41+
42+
int exp_dns_failure = 0;
43+
int exp_ok = 0;
44+
45+
// Invalidate 1st and 3th etc. entries
46+
for (unsigned int i = 0; i < MBED_CONF_APP_DNS_SIMULT_QUERIES + 1; i++) {
47+
if ((i % 2) == 0) {
48+
// Last query fails to no memory so do not increase
49+
if (i != MBED_CONF_APP_DNS_SIMULT_QUERIES) {
50+
exp_dns_failure++;
51+
}
52+
strcat(&(dns_test_hosts_new[i][0]), "_invalid");
53+
} else {
54+
// Last query fails to no memory so do not increase
55+
if (i != MBED_CONF_APP_DNS_SIMULT_QUERIES) {
56+
exp_ok++;
57+
}
58+
}
59+
}
60+
61+
do_asynchronous_gethostbyname(dns_test_hosts_new, MBED_CONF_APP_DNS_SIMULT_QUERIES + 1, &result_ok, &result_no_mem,
62+
&result_dns_failure, &result_exp_timeout);
63+
64+
TEST_ASSERT(result_ok == exp_ok);
65+
TEST_ASSERT(result_no_mem == 1); // last query fails for no memory as expected
66+
TEST_ASSERT(result_dns_failure == exp_dns_failure || result_dns_failure == exp_dns_failure + 1);
67+
TEST_ASSERT(result_exp_timeout == 0);
68+
}

0 commit comments

Comments
 (0)