From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 694D76ECFD; Wed, 2 Dec 2026 11:43:17 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 694D76ECFD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1796200997; bh=1JjOpVvQ+4J6MxjpVBZHn4KXhMjaOrEqhELMsvZsTXc=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=wpspFxjAmmVrzDALI5oZjLGEA6frNODEEKt5sFCvL3c/9SpZ7hmD2dLaILQ6CMFs+ yMd/EtLXpwd73iZtykmUzMiUhEGZsF+V79PgdFvAU6sD0xnEwQbzpgKsxZjxaWJt/C Dp1z6dPO+R443ZTahZivj68BGfNTyB0r0Gg9BPVU= Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id C859B6ECFD for ; Wed, 2 Dec 2026 11:43:16 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C859B6ECFD Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-6bb06dc1fd5so12730272a12.1 for ; Wed, 02 Dec 2026 00:43:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796200996; x=1796805796; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=/siNKJX7xCWKFAkI5T8W3YPQh3tREhEeNxtf4dz9mY0=; b=Z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NR79QrKF2DzmYUEIqZoPT61JvYBOKOcdtfly3EObXvNxR33I0Pj0= X-Received: by 2002:a05:651c:a344:b0:3ac:436e:4084 with SMTP id 38308e7fff4ca-3ae7f9b2126mr122716971fa.4.1796200481096; Wed, 02 Dec 2026 00:34:41 -0800 (PST) Received: from localhost ([178.177.22.10]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-3ae804ee2b0sm97003461fa.11.2026.12.02.00.34.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 00:34:40 -0800 (PST) To: tarantool-patches@dev.tarantool.org, Sergey Kaplun , e.temirgaleev@tarantool.org Date: Wed, 2 Dec 2026 11:34:35 +0300 Message-ID: X-Mailer: git-send-email 2.56.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH v2 luajit 1/3] test: add unit tests for the write buffer X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Sergey Bronnikov The write buffer accumulates the profilers' output and passes it to the writer on flush. It has no unit tests, so cover writing of the data of all kinds, flushing of a full buffer, the explicit flush and the error reporting. Relates to tarantool/tarantool#11620 --- test/tarantool-c-tests/unit-wbuf.test.c | 465 ++++++++++++++++++++++++ 1 file changed, 465 insertions(+) create mode 100644 test/tarantool-c-tests/unit-wbuf.test.c diff --git a/test/tarantool-c-tests/unit-wbuf.test.c b/test/tarantool-c-tests/unit-wbuf.test.c new file mode 100644 index 00000000..9107f2fb --- /dev/null +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -0,0 +1,465 @@ +/* + * Testing of the write buffer. + * + * The write buffer accumulates the profilers' output and passes + * it to the writer on flush. The tests below cover writing of the + * data of all kinds, flushing on overflow, the explicit flush and + * the error reporting. + */ + +#include + +#include +#include +#include + +#include "lj_utils.h" +#include "lj_wbuf.h" +#include "test.h" + +/* Size of the write buffer used in the tests. */ +#define BUFFER_SIZE 10 + +/* Size of the payload that does not fit into the write buffer. */ +#define PAYLOAD_SIZE (2 * BUFFER_SIZE + 5) + +/* What the writer does on flush. */ +enum writer_mode { + /* Report all the data as written. */ + WMODE_OK, + /* + * Report a half of the data as written and keep the + * buffer. + */ + WMODE_PARTIAL, + /* Imitate an unrecoverable write error, e.g. ENOSPC. */ + WMODE_FAIL, +}; + +/* Writer context for simulating different write outcomes. */ +struct writer_ctx { + /* What the writer does on flush. */ + enum writer_mode mode; + /* Number of performed flushes. */ + size_t flushes; + /* Number of bytes reported as written. */ + size_t written; + /* + * Everything reported as written, in order of writing. + * + * Must hold the bytes reported over a whole test. The + * largest amount is produced by + * test_addn_pumped_through_buffer(), which reports + * 2 * BUFFER_SIZE bytes. 2 * PAYLOAD_SIZE is a + * conservative upper bound that leaves some headroom. + */ + uint8_t out[2 * PAYLOAD_SIZE]; +}; + +static size_t test_writer(const void **buf_addr, size_t len, void *opt) +{ + struct writer_ctx *ctx = opt; + size_t n = len; + + ctx->flushes++; + switch (ctx->mode) { + case WMODE_FAIL: + errno = ENOSPC; + *buf_addr = NULL; + return 0; + case WMODE_PARTIAL: + errno = EIO; + n = len / 2; + break; + case WMODE_OK: + break; + default: + /* Unreachable: a test is misconfigured. */ + assert_true(0); + return 0; + } + assert_true(ctx->written + n <= sizeof(ctx->out)); + memcpy(ctx->out + ctx->written, *buf_addr, n); + /* The function is called only by lj_wbuf_flush(). */ + ctx->written += n; + return n; +} + +static void writer_ctx_init(struct writer_ctx *ctx, enum writer_mode mode) +{ + memset(ctx, 0, sizeof(*ctx)); + ctx->mode = mode; +} + +/* + * Declares and initializes the objects shared by all the tests. + * Tests that need extra local variables should declare them + * before the macro invocation. + */ +#define WBUF_TEST_SETUP(mode) \ + uint8_t mem[BUFFER_SIZE]; \ + struct lj_wbuf buf; \ + struct writer_ctx ctx; \ + writer_ctx_init(&ctx, (mode)); \ + lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem)) + +/* + * A copy of wbuf_len() from src/lj_wbuf.c: the original is a + * static function and is not visible to the tests. + */ +static size_t wbuf_len(const struct lj_wbuf *buf) +{ + return (size_t)(buf->pos - buf->buf); +} + +#define assert_wbuf_stopped_enospc(wbuf) do { \ + assert_true(lj_wbuf_test_flag((wbuf), STREAM_STOP)); \ + assert_true(lj_wbuf_test_flag((wbuf), STREAM_ERRIO)); \ + assert_int_equal(lj_wbuf_errno((wbuf)), ENOSPC); \ + assert_ptr_equal((wbuf)->buf, NULL); \ + assert_ptr_equal((wbuf)->pos, NULL); \ +} while (0) + +static int test_addbyte(void *state) +{ + WBUF_TEST_SETUP(WMODE_OK); + + UNUSED(state); + + lj_wbuf_addbyte(&buf, 0x7f); + assert_sizet_equal(wbuf_len(&buf), 1); + assert_int_equal(mem[0], 0x7f); + assert_sizet_equal(ctx.flushes, 0); + assert_int_equal(lj_wbuf_errno(&buf), 0); + + return TEST_EXIT_SUCCESS; +} + +static int test_addu64(void *state) +{ + WBUF_TEST_SETUP(WMODE_OK); + + UNUSED(state); + + /* 128 is encoded as two bytes. */ + lj_wbuf_addu64(&buf, 128); + assert_sizet_equal(wbuf_len(&buf), 2); + assert_int_equal(mem[0], 0x80); + assert_int_equal(mem[1], 0x01); + /* + * addu64() reserves LEB128_U64_MAXSIZE bytes regardless + * of the actual encoded length, so the buffer must be + * large enough to avoid a flush here. + */ + assert_true(LEB128_U64_MAXSIZE <= BUFFER_SIZE); + assert_sizet_equal(ctx.flushes, 0); + + return TEST_EXIT_SUCCESS; +} + +static int test_addn(void *state) +{ + WBUF_TEST_SETUP(WMODE_OK); + + UNUSED(state); + + lj_wbuf_addn(&buf, "xyz", 3); + assert_sizet_equal(wbuf_len(&buf), 3); + assert_true(memcmp(mem, "xyz", 3) == 0); + assert_sizet_equal(ctx.flushes, 0); + + return TEST_EXIT_SUCCESS; +} + +static int test_addstring(void *state) +{ + WBUF_TEST_SETUP(WMODE_OK); + + UNUSED(state); + + /* A string is a ULEB128 length followed by the payload. */ + lj_wbuf_addstring(&buf, "xyz"); + assert_sizet_equal(wbuf_len(&buf), 4); + assert_int_equal(mem[0], 3); + assert_true(memcmp(mem + 1, "xyz", 3) == 0); + /* + * addstring() writes a ULEB128 length via addu64(), which + * reserves LEB128_U64_MAXSIZE bytes, so the buffer must + * be large enough to avoid a flush here. + */ + assert_true(LEB128_U64_MAXSIZE <= BUFFER_SIZE); + assert_sizet_equal(ctx.flushes, 0); + + return TEST_EXIT_SUCCESS; +} + +static int test_addn_pumped_through_buffer(void *state) +{ + uint8_t payload[PAYLOAD_SIZE]; + + WBUF_TEST_SETUP(WMODE_OK); + UNUSED(state); + memset(payload, 'a', sizeof(payload)); + + const int writes = PAYLOAD_SIZE / BUFFER_SIZE; + /* + * The payload does not fit into the buffer, so it is + * pumped through it. Two full buffers are flushed, the + * rest stays in the buffer. + */ + lj_wbuf_addn(&buf, payload, sizeof(payload)); + assert_sizet_equal(ctx.flushes, 2); + assert_sizet_equal(ctx.written, 2 * BUFFER_SIZE); + assert_true(memcmp(ctx.out, payload, 2 * BUFFER_SIZE) == 0); + const int remains = PAYLOAD_SIZE - writes * BUFFER_SIZE; + assert_sizet_equal(wbuf_len(&buf), remains); + assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE, remains) == 0); + /* The implicit flushes completed without an error. */ + assert_false(lj_wbuf_test_flag(&buf, STREAM_ERRIO | STREAM_STOP)); + + return TEST_EXIT_SUCCESS; +} + +static int test_addn_pumped_through_buffer_partial(void *state) +{ + uint8_t payload[PAYLOAD_SIZE]; + + WBUF_TEST_SETUP(WMODE_PARTIAL); + UNUSED(state); + memset(payload, 'a', sizeof(payload)); + + const int writes = PAYLOAD_SIZE / BUFFER_SIZE; + const int remains = PAYLOAD_SIZE - writes * BUFFER_SIZE; + /* + * The writer reports only a half of each flushed buffer + * as written, so the pumping loop loses the unwritten + * halves, but it does not stop on STREAM_ERRIO and keeps + * pumping the rest of the payload. + */ + lj_wbuf_addn(&buf, payload, sizeof(payload)); + assert_sizet_equal(ctx.flushes, writes); + assert_sizet_equal(ctx.written, writes * (BUFFER_SIZE / 2)); + /* Not all the data has been written. */ + assert_true(ctx.written < sizeof(payload)); + assert_true(lj_wbuf_test_flag(&buf, STREAM_ERRIO)); + assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP)); + assert_int_equal(lj_wbuf_errno(&buf), EIO); + /* + * The tail that did not fit into a full buffer is + * buffered. + */ + assert_sizet_equal(wbuf_len(&buf), remains); + assert_true(memcmp(mem, payload + writes * BUFFER_SIZE, remains) == 0); + + return TEST_EXIT_SUCCESS; +} + +static int test_flush_on_overflow_addbyte(void *state) +{ + uint8_t payload[BUFFER_SIZE]; + + WBUF_TEST_SETUP(WMODE_OK); + UNUSED(state); + memset(payload, 'a', sizeof(payload)); + + /* Fill the buffer without a flush. */ + lj_wbuf_addn(&buf, payload, sizeof(payload)); + assert_sizet_equal(wbuf_len(&buf), sizeof(mem)); + + /* + * A byte written to a full buffer is preceded by a flush. + */ + lj_wbuf_addbyte(&buf, 0xaa); + assert_sizet_equal(ctx.flushes, 1); + assert_sizet_equal(ctx.written, sizeof(mem)); + assert_sizet_equal(wbuf_len(&buf), 1); + assert_int_equal(mem[0], 0xaa); + + return TEST_EXIT_SUCCESS; +} + +static int test_flush_on_overflow_addu64(void *state) +{ + uint8_t payload[BUFFER_SIZE]; + + WBUF_TEST_SETUP(WMODE_OK); + UNUSED(state); + memset(payload, 'a', sizeof(payload)); + + /* Fill the buffer without a flush. */ + lj_wbuf_addn(&buf, payload, sizeof(payload)); + assert_sizet_equal(wbuf_len(&buf), sizeof(mem)); + + /* + * An ULEB128 value written to a full buffer is preceded + * by a flush. + */ + lj_wbuf_addu64(&buf, 0); + assert_sizet_equal(ctx.flushes, 1); + assert_sizet_equal(ctx.written, sizeof(mem)); + assert_sizet_equal(wbuf_len(&buf), 1); + /* 0 is encoded as a single 0x00 byte. */ + assert_int_equal(mem[0], 0x00); + + return TEST_EXIT_SUCCESS; +} + +static int test_flush_on_overflow_addn(void *state) +{ + uint8_t payload[BUFFER_SIZE]; + + WBUF_TEST_SETUP(WMODE_OK); + UNUSED(state); + memset(payload, 'a', sizeof(payload)); + + /* Fill the buffer without a flush. */ + lj_wbuf_addn(&buf, payload, sizeof(payload)); + assert_sizet_equal(wbuf_len(&buf), sizeof(mem)); + + /* + * A data block written to a full buffer is preceded by + * a flush. + */ + lj_wbuf_addn(&buf, payload, 1); + assert_sizet_equal(ctx.flushes, 1); + assert_sizet_equal(ctx.written, sizeof(mem)); + assert_sizet_equal(wbuf_len(&buf), 1); + assert_int_equal(mem[0], payload[0]); + + return TEST_EXIT_SUCCESS; +} + +static int test_flush_and_write(void *state) +{ + WBUF_TEST_SETUP(WMODE_OK); + + UNUSED(state); + + lj_wbuf_addn(&buf, "xyz", 3); + lj_wbuf_flush(&buf); + assert_sizet_equal(ctx.flushes, 1); + assert_sizet_equal(ctx.written, 3); + assert_true(memcmp(ctx.out, "xyz", 3) == 0); + /* The buffer is empty after a successful flush. */ + assert_sizet_equal(wbuf_len(&buf), 0); + assert_false(lj_wbuf_test_flag(&buf, + STREAM_ERRIO | STREAM_STOP)); + + /* The buffer is usable after the flush. */ + lj_wbuf_addbyte(&buf, 0x7f); + assert_sizet_equal(wbuf_len(&buf), 1); + assert_int_equal(mem[0], 0x7f); + + return TEST_EXIT_SUCCESS; +} + +static int test_flush_partial_write(void *state) +{ + WBUF_TEST_SETUP(WMODE_PARTIAL); + + UNUSED(state); + + lj_wbuf_addn(&buf, "xyz", 3); + lj_wbuf_flush(&buf); + /* A partial write is reported as an error. */ + assert_true(lj_wbuf_test_flag(&buf, STREAM_ERRIO)); + assert_int_equal(lj_wbuf_errno(&buf), EIO); + /* + * The writer keeps the buffer, so the stream is not + * stopped. + */ + assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP)); + assert_ptr_equal(buf.buf, mem); + /* The buffer is reset after the flush. */ + assert_sizet_equal(wbuf_len(&buf), 0); + + return TEST_EXIT_SUCCESS; +} + +static int test_flush_stopped_stream(void *state) +{ + WBUF_TEST_SETUP(WMODE_FAIL); + + UNUSED(state); + + lj_wbuf_addn(&buf, "xyz", 3); + lj_wbuf_flush(&buf); + assert_wbuf_stopped_enospc(&buf); + assert_sizet_equal(ctx.flushes, 1); + + /* A stopped stream is not flushed again. */ + lj_wbuf_flush(&buf); + assert_sizet_equal(ctx.flushes, 1); + + return TEST_EXIT_SUCCESS; +} + +static int test_write_to_stopped_stream(void *state) +{ + WBUF_TEST_SETUP(WMODE_FAIL); + + UNUSED(state); + + lj_wbuf_addn(&buf, "xyz", 3); + lj_wbuf_flush(&buf); + assert_wbuf_stopped_enospc(&buf); + + /* Writes to a stopped stream are ignored. */ + lj_wbuf_addbyte(&buf, 0x7f); + lj_wbuf_addu64(&buf, 128); + lj_wbuf_addn(&buf, "xyz", 3); + lj_wbuf_addstring(&buf, "xyz"); + assert_wbuf_stopped_enospc(&buf); + assert_sizet_equal(ctx.flushes, 1); + assert_sizet_equal(ctx.written, 0); + + return TEST_EXIT_SUCCESS; +} + +static int test_terminate(void *state) +{ + WBUF_TEST_SETUP(WMODE_OK); + + UNUSED(state); + + lj_wbuf_terminate(&buf); + assert_true(buf.writer == NULL); + assert_ptr_equal(buf.ctx, NULL); + assert_ptr_equal(buf.buf, NULL); + assert_ptr_equal(buf.pos, NULL); + assert_sizet_equal(buf.size, 0); + assert_false(lj_wbuf_test_flag(&buf, + STREAM_ERRIO | STREAM_STOP)); + assert_int_equal(lj_wbuf_errno(&buf), 0); + + return TEST_EXIT_SUCCESS; +} + +/* + * A writer may release the buffer and set *data to NULL on an + * unrecoverable error (e.g. ENOSPC). The stream is stopped in + * this case, so the write functions must not touch the released + * buffer. + */ +int main(void) +{ + const struct test_unit tgroup[] = { + test_unit_def(test_addbyte), + test_unit_def(test_addu64), + test_unit_def(test_addn), + test_unit_def(test_addstring), + test_unit_def(test_addn_pumped_through_buffer), + test_unit_def(test_addn_pumped_through_buffer_partial), + test_unit_def(test_flush_on_overflow_addbyte), + test_unit_def(test_flush_on_overflow_addu64), + test_unit_def(test_flush_on_overflow_addn), + test_unit_def(test_flush_and_write), + test_unit_def(test_flush_partial_write), + test_unit_def(test_flush_stopped_stream), + test_unit_def(test_write_to_stopped_stream), + test_unit_def(test_terminate), + }; + const int test_result = test_run_group(tgroup, NULL); + + return test_result; +} -- 2.56.0