mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-20 05:23:12 -07:00
Add Test for Takion Send Buffer
This commit is contained in:
parent
8914ac5f0b
commit
03b81e9509
6 changed files with 192 additions and 4 deletions
|
@ -23,6 +23,8 @@
|
||||||
#include "thread.h"
|
#include "thread.h"
|
||||||
#include "seqnum.h"
|
#include "seqnum.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
@ -47,12 +49,18 @@ typedef struct chiaki_takion_send_buffer_t
|
||||||
} ChiakiTakionSendBuffer;
|
} ChiakiTakionSendBuffer;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Init a Send Buffer and start a thread that automatically re-sends packets on takion.
|
||||||
|
*
|
||||||
|
* @param takion if NULL, the Send Buffer thread will effectively do nothing (for unit testing)
|
||||||
|
* @param size number of packet slots
|
||||||
|
*/
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_buffer_init(ChiakiTakionSendBuffer *send_buffer, ChiakiTakion *takion, size_t size);
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_buffer_init(ChiakiTakionSendBuffer *send_buffer, ChiakiTakion *takion, size_t size);
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_buffer_start(ChiakiTakionSendBuffer *send_buffer);
|
|
||||||
CHIAKI_EXPORT void chiaki_takion_send_buffer_fini(ChiakiTakionSendBuffer *send_buffer);
|
CHIAKI_EXPORT void chiaki_takion_send_buffer_fini(ChiakiTakionSendBuffer *send_buffer);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param buf ownership of this is taken by the ChiakiTakionSendBuffer, which will free it automatically later!
|
* @param buf ownership of this is taken by the ChiakiTakionSendBuffer, which will free it automatically later!
|
||||||
|
* On error, buf is freed immediately.
|
||||||
*/
|
*/
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_buffer_push(ChiakiTakionSendBuffer *send_buffer, ChiakiSeqNum32 seq_num, uint8_t *buf, size_t buf_size);
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_buffer_push(ChiakiTakionSendBuffer *send_buffer, ChiakiSeqNum32 seq_num, uint8_t *buf, size_t buf_size);
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_buffer_ack(ChiakiTakionSendBuffer *send_buffer, ChiakiSeqNum32 seq_num);
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_buffer_ack(ChiakiTakionSendBuffer *send_buffer, ChiakiSeqNum32 seq_num);
|
||||||
|
|
|
@ -15,6 +15,8 @@
|
||||||
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef CHIAKI_UNIT_TEST
|
||||||
|
|
||||||
#include <chiaki/takionsendbuffer.h>
|
#include <chiaki/takionsendbuffer.h>
|
||||||
#include <chiaki/takion.h>
|
#include <chiaki/takion.h>
|
||||||
#include <chiaki/time.h>
|
#include <chiaki/time.h>
|
||||||
|
@ -22,11 +24,11 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
|
||||||
#define TAKION_DATA_RESEND_TIMEOUT_MS 200
|
#define TAKION_DATA_RESEND_TIMEOUT_MS 200
|
||||||
#define TAKION_DATA_RESEND_WAKEUP_TIMEOUT_MS (TAKION_DATA_RESEND_TIMEOUT_MS/2)
|
#define TAKION_DATA_RESEND_WAKEUP_TIMEOUT_MS (TAKION_DATA_RESEND_TIMEOUT_MS/2)
|
||||||
#define TAKION_DATA_RESEND_TRIES_MAX 10
|
#define TAKION_DATA_RESEND_TRIES_MAX 10
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
struct chiaki_takion_send_buffer_packet_t
|
struct chiaki_takion_send_buffer_packet_t
|
||||||
{
|
{
|
||||||
|
@ -37,12 +39,14 @@ struct chiaki_takion_send_buffer_packet_t
|
||||||
size_t buf_size;
|
size_t buf_size;
|
||||||
}; // ChiakiTakionSendBufferPacket
|
}; // ChiakiTakionSendBufferPacket
|
||||||
|
|
||||||
|
#ifndef CHIAKI_UNIT_TEST
|
||||||
|
|
||||||
static void *takion_send_buffer_thread_func(void *user);
|
static void *takion_send_buffer_thread_func(void *user);
|
||||||
|
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_buffer_init(ChiakiTakionSendBuffer *send_buffer, ChiakiTakion *takion, size_t size)
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_buffer_init(ChiakiTakionSendBuffer *send_buffer, ChiakiTakion *takion, size_t size)
|
||||||
{
|
{
|
||||||
send_buffer->takion = takion;
|
send_buffer->takion = takion;
|
||||||
send_buffer->log = takion->log;
|
send_buffer->log = takion ? takion->log : NULL;
|
||||||
|
|
||||||
send_buffer->packets = calloc(size, sizeof(ChiakiTakionSendBufferPacket));
|
send_buffer->packets = calloc(size, sizeof(ChiakiTakionSendBufferPacket));
|
||||||
if(!send_buffer->packets)
|
if(!send_buffer->packets)
|
||||||
|
@ -132,6 +136,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_send_buffer_push(ChiakiTakionSendBuf
|
||||||
}
|
}
|
||||||
|
|
||||||
beach:
|
beach:
|
||||||
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
|
free(buf);
|
||||||
chiaki_mutex_unlock(&send_buffer->mutex);
|
chiaki_mutex_unlock(&send_buffer->mutex);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
@ -214,6 +220,9 @@ static void *takion_send_buffer_thread_func(void *user)
|
||||||
|
|
||||||
static void takion_send_buffer_resend(ChiakiTakionSendBuffer *send_buffer)
|
static void takion_send_buffer_resend(ChiakiTakionSendBuffer *send_buffer)
|
||||||
{
|
{
|
||||||
|
if(!send_buffer->takion)
|
||||||
|
return;
|
||||||
|
|
||||||
uint64_t now = chiaki_time_now_monotonic_ms();
|
uint64_t now = chiaki_time_now_monotonic_ms();
|
||||||
|
|
||||||
for(size_t i=0; i<send_buffer->packets_count; i++)
|
for(size_t i=0; i<send_buffer->packets_count; i++)
|
||||||
|
@ -229,3 +238,5 @@ static void takion_send_buffer_resend(ChiakiTakionSendBuffer *send_buffer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -10,7 +10,9 @@ add_executable(chiaki-unit
|
||||||
takion.c
|
takion.c
|
||||||
seqnum.c
|
seqnum.c
|
||||||
reorderqueue.c
|
reorderqueue.c
|
||||||
fec.c)
|
fec.c
|
||||||
|
test_log.c
|
||||||
|
test_log.h)
|
||||||
|
|
||||||
target_link_libraries(chiaki-unit chiaki-lib munit)
|
target_link_libraries(chiaki-unit chiaki-lib munit)
|
||||||
|
|
||||||
|
|
112
test/takion.c
112
test/takion.c
|
@ -18,8 +18,14 @@
|
||||||
#include <munit.h>
|
#include <munit.h>
|
||||||
|
|
||||||
#include <chiaki/takion.h>
|
#include <chiaki/takion.h>
|
||||||
|
#include <chiaki/seqnum.h>
|
||||||
#include <chiaki/base64.h>
|
#include <chiaki/base64.h>
|
||||||
|
|
||||||
|
#define CHIAKI_UNIT_TEST
|
||||||
|
#include "../lib/src/takionsendbuffer.c"
|
||||||
|
|
||||||
|
#include "test_log.h"
|
||||||
|
|
||||||
|
|
||||||
static MunitResult test_av_packet_parse(const MunitParameter params[], void *user)
|
static MunitResult test_av_packet_parse(const MunitParameter params[], void *user)
|
||||||
{
|
{
|
||||||
|
@ -67,6 +73,104 @@ static MunitResult test_av_packet_parse_real_video(const MunitParameter params[]
|
||||||
return MUNIT_OK;
|
return MUNIT_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void random_seqnums(ChiakiSeqNum32 *nums, size_t count)
|
||||||
|
{
|
||||||
|
for(size_t i=0; i<count; i++)
|
||||||
|
{
|
||||||
|
ChiakiSeqNum32 seqnum;
|
||||||
|
retry:
|
||||||
|
seqnum = munit_rand_uint32();
|
||||||
|
for(size_t j=0; j<i; j++)
|
||||||
|
{
|
||||||
|
if(nums[j] == seqnum)
|
||||||
|
goto retry;
|
||||||
|
}
|
||||||
|
nums[i] = seqnum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool check_send_buffer_contents(ChiakiTakionSendBuffer *send_buffer, const ChiakiSeqNum32 *nums_expected, size_t nums_expected_count)
|
||||||
|
{
|
||||||
|
// nums_expected must be unique
|
||||||
|
|
||||||
|
if(chiaki_mutex_lock(&send_buffer->mutex) != CHIAKI_ERR_SUCCESS)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if(send_buffer->packets_count != nums_expected_count)
|
||||||
|
goto fail;
|
||||||
|
|
||||||
|
for(size_t i=0; i<nums_expected_count; i++)
|
||||||
|
{
|
||||||
|
bool found = false;
|
||||||
|
for(size_t j=0; j<send_buffer->packets_count; j++)
|
||||||
|
{
|
||||||
|
if(send_buffer->packets[j].seq_num == nums_expected[i])
|
||||||
|
{
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!found)
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
chiaki_mutex_unlock(&send_buffer->mutex);
|
||||||
|
return true;
|
||||||
|
fail:
|
||||||
|
chiaki_mutex_unlock(&send_buffer->mutex);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void seqnums_ack(ChiakiSeqNum32 *nums, size_t *nums_count, ChiakiSeqNum32 ack_num)
|
||||||
|
{
|
||||||
|
// simulate ack of ack_num
|
||||||
|
for(size_t i=0; i<*nums_count; i++)
|
||||||
|
{
|
||||||
|
if(nums[i] == ack_num || chiaki_seq_num_32_lt(nums[i], ack_num))
|
||||||
|
{
|
||||||
|
for(size_t j=i+1; j<*nums_count; j++)
|
||||||
|
nums[j-1] = nums[j];
|
||||||
|
(*nums_count)--;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static MunitResult test_takion_send_buffer(const MunitParameter params[], void *user)
|
||||||
|
{
|
||||||
|
static const size_t nums_count = 0x30;
|
||||||
|
ChiakiTakionSendBuffer send_buffer;
|
||||||
|
ChiakiErrorCode err = chiaki_takion_send_buffer_init(&send_buffer, NULL, nums_count);
|
||||||
|
munit_assert_int(err, ==, CHIAKI_ERR_SUCCESS);
|
||||||
|
send_buffer.log = get_test_log();
|
||||||
|
|
||||||
|
ChiakiSeqNum32 nums_expected[nums_count + 1];
|
||||||
|
random_seqnums(nums_expected, nums_count + 1);
|
||||||
|
|
||||||
|
for(size_t i=0; i<nums_count; i++)
|
||||||
|
{
|
||||||
|
err = chiaki_takion_send_buffer_push(&send_buffer, nums_expected[i], malloc(8), 8);
|
||||||
|
munit_assert_int(err, ==, CHIAKI_ERR_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = chiaki_takion_send_buffer_push(&send_buffer, nums_expected[nums_count], malloc(8), 8);
|
||||||
|
munit_assert_int(err, ==, CHIAKI_ERR_OVERFLOW);
|
||||||
|
|
||||||
|
size_t nums_count_cur = nums_count;
|
||||||
|
while(nums_count_cur > 0)
|
||||||
|
{
|
||||||
|
ChiakiSeqNum32 ack_num = nums_expected[nums_count_cur - 1]
|
||||||
|
+ munit_rand_int_range(-1, 1) * munit_rand_int_range(1, 32);
|
||||||
|
chiaki_takion_send_buffer_ack(&send_buffer, ack_num);
|
||||||
|
seqnums_ack(nums_expected, &nums_count_cur, ack_num);
|
||||||
|
bool correct = check_send_buffer_contents(&send_buffer, nums_expected, nums_count_cur);
|
||||||
|
munit_assert(correct);
|
||||||
|
}
|
||||||
|
|
||||||
|
chiaki_takion_send_buffer_fini(&send_buffer);
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
MunitTest tests_takion[] = {
|
MunitTest tests_takion[] = {
|
||||||
|
@ -86,5 +190,13 @@ MunitTest tests_takion[] = {
|
||||||
MUNIT_TEST_OPTION_NONE,
|
MUNIT_TEST_OPTION_NONE,
|
||||||
NULL
|
NULL
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"/send_buffer",
|
||||||
|
test_takion_send_buffer,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
MUNIT_TEST_OPTION_NONE,
|
||||||
|
NULL
|
||||||
|
},
|
||||||
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||||
};
|
};
|
30
test/test_log.c
Normal file
30
test/test_log.c
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Chiaki.
|
||||||
|
*
|
||||||
|
* Chiaki is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Chiaki is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "test_log.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
static bool initialized = false;
|
||||||
|
static ChiakiLog log_quiet;
|
||||||
|
|
||||||
|
ChiakiLog *get_test_log()
|
||||||
|
{
|
||||||
|
if(!initialized)
|
||||||
|
chiaki_log_init(&log_quiet, 0, NULL, NULL);
|
||||||
|
return &log_quiet;
|
||||||
|
}
|
25
test/test_log.h
Normal file
25
test/test_log.h
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
/*
|
||||||
|
* This file is part of Chiaki.
|
||||||
|
*
|
||||||
|
* Chiaki is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Chiaki is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef CHIAKI_TEST_LOG_H
|
||||||
|
#define CHIAKI_TEST_LOG_H
|
||||||
|
|
||||||
|
#include <chiaki/log.h>
|
||||||
|
|
||||||
|
ChiakiLog *get_test_log();
|
||||||
|
|
||||||
|
#endif // CHIAKI_LOG_QUIET_H
|
Loading…
Add table
Add a link
Reference in a new issue