Skip to content

Commit

Permalink
hil: add test for multipart upload
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Szczys <[email protected]>
  • Loading branch information
szczys committed Feb 15, 2025
1 parent 8844144 commit 92d0f89
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
88 changes: 88 additions & 0 deletions tests/hil/tests/stream/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
LOG_TAG_DEFINE(test_stream);

static golioth_sys_sem_t connected_sem;
static golioth_sys_sem_t multi_block_sem;

static void on_client_event(struct golioth_client *client,
enum golioth_client_event event,
Expand Down Expand Up @@ -85,9 +86,90 @@ void test_block_upload(struct golioth_client *client)
}
}

void on_multi_block(struct golioth_client *client,
enum golioth_status status,
const struct golioth_coap_rsp_code *coap_rsp_code,
const char *path,
void *arg)
{
GLTH_LOGI(TAG,
"Multi-block callback; Status: %d, CoAP: %d.%02d",
status,
coap_rsp_code->code_class,
coap_rsp_code->code_detail);

golioth_sys_sem_give(multi_block_sem);
}

void test_multi_block_upload(struct golioth_client *client)
{
enum golioth_status status;
size_t bu_offset, bu_data_len, bu_data_remaining;
uint32_t block_idx = 0;
bool is_last = false;

struct blockwise_transfer *ctx = golioth_stream_blockwise_start("multi_upload",
GOLIOTH_CONTENT_TYPE_JSON,
on_multi_block,
NULL);

if (!ctx)
{
GLTH_LOGE(TAG, "Error allocating context");
}

while (true)
{
bu_offset = block_idx * CONFIG_GOLIOTH_BLOCKWISE_DOWNLOAD_MAX_BLOCK_SIZE;
bu_data_len = CONFIG_GOLIOTH_BLOCKWISE_DOWNLOAD_MAX_BLOCK_SIZE;
bu_data_remaining = test_data_json_len - bu_offset;

if (bu_data_remaining <= CONFIG_GOLIOTH_BLOCKWISE_DOWNLOAD_MAX_BLOCK_SIZE)
{
bu_data_len = bu_data_remaining;
is_last = true;
}

GLTH_LOGI(TAG,
"block-idx: %" PRIu32 " bu_offset: %zu bytes_remaining: %zu",
block_idx,
bu_offset,
bu_data_remaining);


status = golioth_stream_blockwise_set_block_async(client,
ctx,
block_idx,
((uint8_t *) test_data_json) + bu_offset,
bu_data_len,
is_last);

if (status)
{
GLTH_LOGE(TAG, "Error during multi-block upload: %d", status);
goto cleanup;
}

golioth_sys_sem_take(multi_block_sem, GOLIOTH_SYS_WAIT_FOREVER);

if (is_last)
{
break;
}

block_idx += 1;
}

GLTH_LOGI(TAG, "Multi-block upload successful!");

cleanup:
golioth_stream_blockwise_stop(ctx);
}

void hil_test_entry(const struct golioth_client_config *config)
{
connected_sem = golioth_sys_sem_create(1, 0);
multi_block_sem = golioth_sys_sem_create(1, 0);

golioth_debug_set_cloud_log_enabled(false);

Expand All @@ -100,6 +182,12 @@ void hil_test_entry(const struct golioth_client_config *config)
golioth_sys_msleep(1000);

test_block_upload(client);
test_multi_block_upload(client);

/* Wait for stream to propagate from CDG to LightDB Stream */
golioth_sys_msleep(6000);

GLTH_LOGI(TAG, "All stream data has been sent");

/* Let log messages process before returning */
golioth_sys_msleep(5000);
Expand Down
24 changes: 23 additions & 1 deletion tests/hil/tests/stream/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os

BLOCK_UPLOAD_PATH='block_upload'
MULTI_BLOCK_UPLOAD_PATH='multi_upload'

pytestmark = pytest.mark.anyio

Expand All @@ -17,6 +18,9 @@ async def setup(board, device):
# Confirm connection to Golioth
assert None != await board.wait_for_regex_in_line('Golioth CoAP client connected', timeout_s=120)

# Wait for device to finish streaming data for test
assert None != await board.wait_for_regex_in_line('All stream data has been sent', timeout_s=30)

def hash_from_dict(dict_to_hash: dict) -> str:
str_data = json.dumps(dict_to_hash, sort_keys=True)

Expand All @@ -37,7 +41,6 @@ def get_test_data_hash() -> str:
##### Tests #####

async def test_block_upload(board, device):
assert None != await board.wait_for_regex_in_line('Block upload successful', timeout_s=20)

# Wait for stream to propagate from CDG to LightDB Stream
await trio.sleep(6)
Expand All @@ -59,3 +62,22 @@ async def test_block_upload(board, device):

# Assert that a hash of the block upload matches our expected hash
assert blockwise_hash == test_hash

async def test_multi_block_upload(board, device):
# Get Stream entry of the block upload from cloud
stream = await device.stream.get(path=MULTI_BLOCK_UPLOAD_PATH, interval="1m")
print(stream)
assert 'list' in stream and len(stream.keys()) > 0

blockwise_json = stream['list'][0]['data'][MULTI_BLOCK_UPLOAD_PATH]

# Get hash of the JSON string we received
blockwise_hash = hash_from_dict(blockwise_json)
print("Golioth data hash:", blockwise_hash)

# Generate hash from the source JSON file
test_hash = get_test_data_hash()
print("Test data hash:", test_hash)

# Assert that a hash of the block upload matches our expected hash
assert blockwise_hash == test_hash

0 comments on commit 92d0f89

Please sign in to comment.