Knight KE / Mbed OS Game_Master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers asynchronous_dns_external_event_queue.cpp Source File

asynchronous_dns_external_event_queue.cpp

00001 /*
00002  * Copyright (c) 2018, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "mbed.h"
00019 #include "greentea-client/test_env.h"
00020 #include "unity.h"
00021 #include "utest.h"
00022 #include "dns_tests.h"
00023 
00024 #include "nsapi_dns.h"
00025 
00026 using namespace utest::v1;
00027 
00028 namespace {
00029 int result_ok;
00030 int result_no_mem;
00031 int result_dns_failure;
00032 int result_exp_timeout;
00033 
00034 const int EXTERNAL_THREAD_SIZE = 2048;
00035 const int EVENT_QUEUE_SIZE = 10;
00036 
00037 events::EventQueue *event_queue;
00038 }
00039 
00040 static nsapi_error_t event_queue_call(int delay, mbed::Callback<void()> func)
00041 {
00042     if (delay) {
00043         if (event_queue->call_in(delay, func) == 0) {
00044             return NSAPI_ERROR_NO_MEMORY ;
00045         }
00046     } else {
00047         if (event_queue->call(func) == 0) {
00048             return NSAPI_ERROR_NO_MEMORY ;
00049         }
00050     }
00051     return NSAPI_ERROR_OK ;
00052 }
00053 
00054 void ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE()
00055 {
00056     // Ensures that cache does not contain entries
00057     do_asynchronous_gethostbyname(dns_test_hosts, MBED_CONF_NSAPI_DNS_CACHE_SIZE, &result_ok, &result_no_mem,
00058                                   &result_dns_failure, &result_exp_timeout);
00059 
00060     TEST_ASSERT(result_ok == MBED_CONF_NSAPI_DNS_CACHE_SIZE);
00061     TEST_ASSERT(result_no_mem == 0);
00062     TEST_ASSERT(result_dns_failure == 0);
00063     TEST_ASSERT(result_exp_timeout == 0);
00064 
00065     // Dispatch event queue
00066     Thread eventThread(osPriorityNormal, EXTERNAL_THREAD_SIZE);
00067     EventQueue queue(EVENT_QUEUE_SIZE * EVENTS_EVENT_SIZE);
00068     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
00069     event_queue = &queue;
00070 
00071     nsapi_dns_call_in_set(event_queue_call);
00072 
00073     do_asynchronous_gethostbyname(dns_test_hosts_second, MBED_CONF_APP_DNS_SIMULT_QUERIES + 1, &result_ok, &result_no_mem,
00074                                   &result_dns_failure, &result_exp_timeout);
00075 
00076     TEST_ASSERT(result_ok == MBED_CONF_APP_DNS_SIMULT_QUERIES);
00077     TEST_ASSERT(result_no_mem == 1); // last query fails for no memory as expected
00078     TEST_ASSERT(result_dns_failure == 0);
00079     TEST_ASSERT(result_exp_timeout == 0);
00080 
00081     // Give event queue time to finalise before destructors
00082     wait(2.0);
00083 
00084     nsapi_dns_call_in_set(0);
00085 }