samples: net: http_client: Allow Docker based testing
Add support for CONFIG_NET_SAMPLE_SEND_ITERATIONS option so that
this sample can be run multiple times against HTTP(s) server
that is running inside a Docker container.
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
diff --git a/samples/net/sockets/http_client/Kconfig b/samples/net/sockets/http_client/Kconfig
new file mode 100644
index 0000000..920d1de
--- /dev/null
+++ b/samples/net/sockets/http_client/Kconfig
@@ -0,0 +1,15 @@
+# Private config options for http-client sample app
+
+# Copyright (c) 2020 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+mainmenu "Networking http-client sample application"
+
+config NET_SAMPLE_SEND_ITERATIONS
+ int "Send a sample HTTP query this many times"
+ default 0
+ help
+ Send a sample HTTP query this many times before exiting. A value of
+ zero means that the sample application in default way.
+
+source "Kconfig.zephyr"
diff --git a/samples/net/sockets/http_client/src/main.c b/samples/net/sockets/http_client/src/main.c
index e4399e7..33ea253 100644
--- a/samples/net/sockets/http_client/src/main.c
+++ b/samples/net/sockets/http_client/src/main.c
@@ -147,13 +147,13 @@
return ret;
}
-void main(void)
+static int run_queries(void)
{
struct sockaddr_in6 addr6;
struct sockaddr_in addr4;
int sock4 = -1, sock6 = -1;
int32_t timeout = 3 * MSEC_PER_SEC;
- int ret;
+ int ret = 0;
int port = HTTP_PORT;
if (IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS)) {
@@ -164,7 +164,7 @@
if (ret < 0) {
LOG_ERR("Failed to register public certificate: %d",
ret);
- exit(1);
+ return ret;
}
port = HTTPS_PORT;
@@ -184,7 +184,7 @@
if (sock4 < 0 && sock6 < 0) {
LOG_ERR("Cannot create HTTP connection.");
- exit(1);
+ return -ECONNABORTED;
}
if (sock4 >= 0 && IS_ENABLED(CONFIG_NET_IPV4)) {
@@ -240,7 +240,7 @@
if (sock4 < 0 && sock6 < 0) {
LOG_ERR("Cannot create HTTP connection.");
- exit(1);
+ return -ECONNABORTED;
}
if (sock4 >= 0 && IS_ENABLED(CONFIG_NET_IPV4)) {
@@ -302,7 +302,7 @@
if (sock4 < 0 && sock6 < 0) {
LOG_ERR("Cannot create HTTP connection.");
- exit(1);
+ return -ECONNABORTED;
}
if (sock4 >= 0 && IS_ENABLED(CONFIG_NET_IPV4)) {
@@ -353,5 +353,34 @@
close(sock6);
}
- k_sleep(K_FOREVER);
+ return ret;
+}
+
+void main(void)
+{
+ int iterations = CONFIG_NET_SAMPLE_SEND_ITERATIONS;
+ int i = 0;
+ int ret;
+
+ while (iterations == 0 || i < iterations) {
+ ret = run_queries();
+ if (ret < 0) {
+ exit(1);
+ }
+
+ if (iterations > 0) {
+ i++;
+ if (i >= iterations) {
+ break;
+ }
+ } else {
+ break;
+ }
+ }
+
+ if (iterations == 0) {
+ k_sleep(K_FOREVER);
+ }
+
+ exit(0);
}