Skip to content

Commit cd772b3

Browse files
JordanYateskartben
authored andcommitted
tests: bsim: bluetooth: central: conn timeout under load
Validate that the connection timeout code path works as expected when the command buffer pool is under stress. Signed-off-by: Jordan Yates <[email protected]>
1 parent 7365bcf commit cd772b3

File tree

1 file changed

+21
-3
lines changed
  • tests/bsim/bluetooth/host/central/src

1 file changed

+21
-3
lines changed

tests/bsim/bluetooth/host/central/src/main.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "bstests.h"
88
#include "babblekit/testcase.h"
99
#include <zephyr/bluetooth/conn.h>
10+
#include <zephyr/bluetooth/hci.h>
1011

1112
/* Include conn_internal for the purpose of checking reference counts. */
1213
#include "host/conn_internal.h"
@@ -42,7 +43,7 @@ static struct bt_conn_cb conn_cb = {
4243
.connected = connected_cb,
4344
};
4445

45-
static void test_central_connect_timeout_with_timeout(uint32_t timeout_ms)
46+
static void test_central_connect_timeout_with_timeout(uint32_t timeout_ms, bool stack_load)
4647
{
4748
int err;
4849
struct bt_conn *conn;
@@ -62,6 +63,7 @@ static void test_central_connect_timeout_with_timeout(uint32_t timeout_ms)
6263
.window_coded = 0,
6364
.timeout = timeout_ms / 10,
6465
};
66+
struct net_buf *bufs[CONFIG_BT_BUF_CMD_TX_COUNT];
6567

6668
k_sem_reset(&sem_failed_to_connect);
6769

@@ -70,6 +72,21 @@ static void test_central_connect_timeout_with_timeout(uint32_t timeout_ms)
7072
err = bt_conn_le_create(&peer, &create_param, BT_LE_CONN_PARAM_DEFAULT, &conn);
7173
TEST_ASSERT(err == 0, "Failed starting initiator (err %d)", err);
7274

75+
if (stack_load) {
76+
/* Claim all the buffers so that the stack cannot handle the timeout */
77+
for (int i = 0; i < CONFIG_BT_BUF_CMD_TX_COUNT; i++) {
78+
bufs[i] = bt_hci_cmd_create(BT_HCI_LE_ADV_ENABLE, 0);
79+
TEST_ASSERT(bufs[i] != NULL, "Failed to claim all command buffers");
80+
}
81+
/* Hold all the buffers until after we expect the connection to timeout */
82+
err = k_sem_take(&sem_failed_to_connect, K_MSEC(expected_conn_timeout_ms + 50));
83+
TEST_ASSERT(err == -EAGAIN, "Callback ran with no buffers available", err);
84+
/* Release all the buffers back to the stack */
85+
for (int i = 0; i < CONFIG_BT_BUF_CMD_TX_COUNT; i++) {
86+
net_buf_unref(bufs[i]);
87+
}
88+
}
89+
7390
err = k_sem_take(&sem_failed_to_connect, K_MSEC(2 * expected_conn_timeout_ms));
7491
TEST_ASSERT(err == 0, "Failed getting connected timeout within %d s (err %d)",
7592
2 * expected_conn_timeout_ms, err);
@@ -96,8 +113,9 @@ static void test_central_connect_timeout(void)
96113
err = bt_enable(NULL);
97114
TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err);
98115

99-
test_central_connect_timeout_with_timeout(0);
100-
test_central_connect_timeout_with_timeout(1000);
116+
test_central_connect_timeout_with_timeout(0, false);
117+
test_central_connect_timeout_with_timeout(1000, false);
118+
test_central_connect_timeout_with_timeout(2000, true);
101119

102120
TEST_PASS("Correct timeout");
103121
}

0 commit comments

Comments
 (0)