takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tcpsocket_open_limit.cpp Source File

tcpsocket_open_limit.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 "greentea-client/test_env.h"
00019 #include "mbed.h"
00020 #include "tcp_tests.h"
00021 #include "TCPSocket.h"
00022 #include "unity/unity.h"
00023 #include "utest.h"
00024 
00025 using namespace utest::v1;
00026 
00027 namespace {
00028 typedef struct TCPSocketItem {
00029     TCPSocket *sock;
00030     TCPSocketItem *next;
00031 } SocketItem;
00032 }
00033 
00034 void TCPSOCKET_OPEN_LIMIT()
00035 {
00036     int open_sockets[2] = {0};
00037 
00038     for (int i = 0; i < 2; i++) {
00039         TCPSocketItem *socket_list_head = NULL;
00040         TCPSocketItem *it;
00041 
00042         TCPSocket *sock;
00043         int ret;
00044         while (true) {
00045             sock = new TCPSocket;
00046             if (!sock) {
00047                 break;
00048             }
00049             ret = sock->open(get_interface());
00050             if (ret == NSAPI_ERROR_NO_MEMORY  || ret == NSAPI_ERROR_NO_SOCKET ) {
00051                 printf("[round#%02d] unable to open new socket, error: %d\n", i, ret);
00052                 delete sock;
00053                 break;
00054             }
00055 
00056             // Hopefully this doesn't interfere when trying to allocate more sockets
00057             it = new TCPSocketItem;
00058             if (!it) {
00059                 delete sock;
00060                 break;
00061             }
00062 
00063             it->sock = sock;
00064             // Order of items in the list doesn't matter
00065             it->next = socket_list_head;
00066             socket_list_head = it;
00067         }
00068 
00069         if (!socket_list_head) {
00070             break;
00071         }
00072 
00073         TCPSocketItem *tmp;
00074         for (TCPSocketItem *it = socket_list_head; it;) {
00075             ++open_sockets[i];
00076             tmp = it;
00077             it = it->next;
00078             socket_list_head = it;
00079             delete tmp->sock;
00080             delete tmp;
00081         }
00082         printf("[round#%02d] %d sockets opened\n", i, open_sockets[i]);
00083     }
00084     TEST_ASSERT_EQUAL(open_sockets[0], open_sockets[1]);
00085     TEST_ASSERT(open_sockets[0] >= 4);
00086 }