takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers asynchronous_dns_timeouts.cpp Source File

asynchronous_dns_timeouts.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     // Run event queue without delays
00043     if (event_queue->call(func) == 0) {
00044         return NSAPI_ERROR_NO_MEMORY ;
00045     }
00046 
00047     return NSAPI_ERROR_OK ;
00048 }
00049 
00050 void ASYNCHRONOUS_DNS_TIMEOUTS()
00051 {
00052     // Ensures that cache does not contain entries
00053     do_asynchronous_gethostbyname(dns_test_hosts, MBED_CONF_NSAPI_DNS_CACHE_SIZE, &result_ok, &result_no_mem,
00054                                   &result_dns_failure, &result_exp_timeout);
00055 
00056     TEST_ASSERT(result_ok == MBED_CONF_NSAPI_DNS_CACHE_SIZE);
00057     TEST_ASSERT(result_no_mem == 0);
00058     TEST_ASSERT(result_dns_failure == 0);
00059     TEST_ASSERT(result_exp_timeout == 0);
00060 
00061     // Dispatch event queue
00062     Thread eventThread(osPriorityNormal, EXTERNAL_THREAD_SIZE);
00063     EventQueue queue(EVENT_QUEUE_SIZE * EVENTS_EVENT_SIZE);
00064     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
00065     event_queue = &queue;
00066 
00067     // Set event queue callback to test specific one
00068     nsapi_dns_call_in_set(event_queue_call);
00069 
00070     do_asynchronous_gethostbyname(dns_test_hosts_second, MBED_CONF_APP_DNS_SIMULT_QUERIES + 1, &result_ok, &result_no_mem,
00071                                   &result_dns_failure, &result_exp_timeout);
00072 
00073     // Depends on timing, but at least one operation shall fail to timeout
00074     TEST_ASSERT(result_exp_timeout > 0);
00075 
00076     // Give event queue time to finalise before destructors
00077     wait(2.0);
00078 
00079     nsapi_dns_call_in_set(0);
00080 }