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 3F16A6ECF9; Tue, 1 Dec 2026 18:11:38 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3F16A6ECF9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1796137898; bh=Ud8mEd4VOFj0IXHLBe+A3MowbMIZShjVnSa4co+5HNI=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=skFYGjZXZukzh3oNZRewHhnwvUbM+1lPjMkhQEWDgjrt5rl9UPoS+emuCFIwYPk/E Fr3PnEl4ZmTqNXbjW4zO8wocLD21wjgOwQYgNaSg/62f/UCk/k5RHWrvYHY39GkvgL BdhmHvjJsvVR2RuhKNtkVagmrVFkvfiMlg/Q/TjE= Received: from send242.i.mail.ru (send242.i.mail.ru [95.163.59.81]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 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 CA4376ECF9 for ; Tue, 1 Dec 2026 18:11:36 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CA4376ECF9 Received: by exim-smtp-7cdd59bc4f-nczz5 with esmtpa (envelope-from ) id 1xYPW3-000000006px-2uM3; Tue, 01 Dec 2026 18:11:36 +0300 Message-ID: <615aec10-fde7-4782-aa63-f3a075d08b71@tarantool.org> Date: Tue, 1 Dec 2026 18:11:35 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: Sergey Kaplun , Sergey Bronnikov References: <4dd8dd2aecca35784e687f7920e6880ad0211f3c.1793624427.git.estetus@gmail.com> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9D183B3A831A946F47DF3C7E45974416C6466C75651961A31182A05F53808504041253BFA0FEBA9B63DE06ABAFEAF67050FFF6DC57A7F876BC68D5F50137C887EED52766DCE320BCF X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7FBB2043146276655EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637ECAF4C2CEE0D0B2F8638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B299190E501D602D272E070BE324C7D3C416F1039DF9DBA178F6B57BC7E64490611E7FA7ABCAF51C92176DF2183F8FC7C078FCF50C7EAF9C588941B15DA834481F9449624AB7ADAF37BA3038C0950A5D36D5E8D9A59859A8B65634FDADD98FB26476E601842F6C81A1F004C906525384303E02D724532EE2C3F43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7C8BDE37D78FCB031643847C11F186F3C59DAA53EE0834AAEE X-C1DE0DAB: 0D63561A33F958A5E3BAA96DE6D559C05002B1117B3ED696B8E758C9069EBB8E03803A57F48E4E5A823CB91A9FED034534781492E4B8EEAD21D4E6D365FE45D1BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE1918E10F71CB4DF9F96AB70F9BE574AE9C625B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D659B38109F8D5048A427A88E1FDED30EF11A4792A5C04F9EE5BD45B77B1CA886DED33CF6749DE0F8148B8341EE9D5BE9A0A95CD049F19CB7A1E3A23214CBF0F466BBE5A26DB7FFE91EDC7CEAA0681F5848F4C41F94D744909CE2512F26BEC029E55448553D2254B8D95CD72808BE417F3B9E0E7457915DAA85F X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXOcIETfglQORZ0zpDET4Zrk3igikrdHlWLp13B7m8B3/l/VZul3y37o= X-Mailru-Sender: 520A125C2F17F0B17094CDC02B85F11B5476BF6640BF3A4F3DE06ABAFEAF67050FFF6DC57A7F876BB7CBEF92542CD7C8795FA72BAB74744FC77752E0C033A69EA16A481184E8BB1C9B38E6EA4F046BE03A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH 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 Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi, Sergey, thanks for review! Please see my comments below. Sergey On 11/5/26 21:39, Sergey Kaplun wrote: > Hi, Sergey! > Thanks for the patch and painstaking work with coverage of wbuf module! > Generally LGTM with minor suggestions below. > > On 02.11.26, Sergey Bronnikov wrote: >> 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. >> >> Part of tarantool/tarantool#11620 > Minor: I'm not sure that this is the part of this issue. Maybe > | Relates to > is more appropriate. Fixed. > >> --- >> test/tarantool-c-tests/unit-wbuf.test.c | 388 ++++++++++++++++++++++++ >> 1 file changed, 388 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..4bee6243 >> --- /dev/null >> +++ b/test/tarantool-c-tests/unit-wbuf.test.c >> @@ -0,0 +1,388 @@ >> +/* >> +** 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. > Minor: It is better to use > | /* > | * > | */ > > Comments here as in the most C/C++ code in the tests. > The inconsistent comment style in the is my fault > and should be fixed. Updated: --- a/test/tarantool-c-tests/unit-wbuf.test.c +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -1,11 +1,11 @@  /* -** 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. -*/ + * 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 >> + >> +#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. */ > Nit: Comment length is more than 66 symbols. > Here and in: > * `test_flush_partial_write()` > * `test_addbyte_failed_flush()` > * `test_addu64_failed_flush()` > * `test_addn_failed_flush()` > * `test_addn_failed_flush_in_loop()` Fixed. > >> + 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. */ >> + uint8_t out[2 * PAYLOAD_SIZE]; > Why do we need 2x size from the PAYLOAD here? Please add the comment. Updated: --- a/test/tarantool-c-tests/unit-wbuf.test.c +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -44,7 +44,15 @@ struct writer_ctx {         size_t flushes;         /* Number of bytes reported as written. */         size_t written; -       /* Everything reported as written, in order of writing. */ +       /* +        * Everything reported as written, in order of writing. +        * +        * Must hold the bytes reported over a whole test. The +        * largest amount is produced by test_flush_on_overflow(), +        * which reports 3 * BUFFER_SIZE bytes. 2 * PAYLOAD_SIZE +        * (4 * BUFFER_SIZE + 10) 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++; > Minor: Please add the comment that writer function is called only by > `lj_wbuf_flush()` before this increment. --- a/test/tarantool-c-tests/unit-wbuf.test.c +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -63,6 +63,7 @@ static size_t test_writer(const void **buf_addr, size_t len, void *opt)                 n = len / 2;         }         memcpy(ctx->out + ctx->written, *buf_addr, n); +       /* The function is called only by lj_wbuf_flush(). */         ctx->written += n;         return n;  } > >> + if (ctx->mode == WMODE_FAIL) { >> + errno = ENOSPC; >> + *buf_addr = NULL; >> + return 0; >> + } >> + if (ctx->mode == WMODE_PARTIAL) { > Minor: s/if/else if/ --- a/test/tarantool-c-tests/unit-wbuf.test.c +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -58,11 +58,12 @@ static size_t test_writer(const void **buf_addr, size_t len, void *opt)                 *buf_addr = NULL;                 return 0;         } -       if (ctx->mode == WMODE_PARTIAL) { +       else if (ctx->mode == WMODE_PARTIAL) {                 errno = EIO;                 n = len / 2;         } >> + errno = EIO; >> + n = len / 2; >> + } >> + memcpy(ctx->out + ctx->written, *buf_addr, n); >> + ctx->written += n; >> + return n; >> +} >> + > > >> + >> +static void assert_stream_stopped_with_error(const struct lj_wbuf *buf) > I suggest using macro instead for such helpers to preserve the line of > original assertion check: > > So instead of: > | not ok 9 - test_flush_stopped_stream > | --- > | location: test/tarantool-c-tests/unit-wbuf.test.c:80 > | condition_failed: 'lj_wbuf_test_flag(buf, STREAM_STOP)' > > We have: > | not ok 9 - test_flush_stopped_stream > | --- > | location: test/tarantool-c-tests/unit-wbuf.test.c:304 > | condition_failed: 'lj_wbuf_test_flag((&buf), STREAM_STOP)' Added: --- a/test/tarantool-c-tests/unit-wbuf.test.c +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -112,14 +112,13 @@ static size_t wbuf_len(const struct lj_wbuf *buf)         return (size_t)(buf->pos - buf->buf);  } -static void assert_stream_stopped_with_error(const struct lj_wbuf *buf) -{ -       assert_true(lj_wbuf_test_flag(buf, STREAM_STOP)); -       assert_true(lj_wbuf_test_flag(buf, STREAM_ERRIO)); -       assert_int_equal(lj_wbuf_errno(buf), ENOSPC); -       assert_ptr_equal(buf->buf, NULL); -       assert_ptr_equal(buf->pos, NULL); -} +#define assert_stream_stopped_with_error(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)  { >> +{ >> + assert_true(lj_wbuf_test_flag(buf, STREAM_STOP)); >> + assert_true(lj_wbuf_test_flag(buf, STREAM_ERRIO)); >> + assert_int_equal(lj_wbuf_errno(buf), ENOSPC); > Since this precise assertion, I suggest naming the helper function/macro > something like `assert_wbuf_stopped_enospc()`. > Added: +#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) + >> + assert_ptr_equal(buf->buf, NULL); >> + assert_ptr_equal(buf->pos, NULL); >> +} >> + >> +static int test_addbyte(void *state) > > > >> +static int test_addstring(void *state) >> +{ > > >> +} >> + >> +static int test_addn_pumped_through_buffer(void *state) >> +{ >> + uint8_t mem[BUFFER_SIZE]; >> + uint8_t payload[PAYLOAD_SIZE]; >> + struct lj_wbuf buf; >> + struct writer_ctx ctx; >> + >> + UNUSED(state); >> + >> + memset(payload, 'a', sizeof(payload)); >> + writer_ctx_init(&ctx, WMODE_OK); >> + lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem)); >> + >> + /* >> + * 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); > This `2` may be not very obvious. > We may declare constant in this function before the comment above: > > | const int writes = PAYLOAD_SIZE / BUFFER_SIZE; Fixed: @@ -181,6 +182,7 @@ static int test_addn_pumped_through_buffer(void *state)         writer_ctx_init(&ctx, WMODE_OK);         lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem)); +       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 > >> + assert_sizet_equal(ctx.written, 2 * BUFFER_SIZE); >> + assert_true(memcmp(ctx.out, payload, 2 * BUFFER_SIZE) == 0); >> + assert_sizet_equal(buf_len(&buf), PAYLOAD_SIZE - 2 * BUFFER_SIZE); > It is better to add the variable: > > | const int remains = PAYLOAD_SIZE - writes * BUFFER_SIZE; Updated: @@ -190,9 +192,9 @@ static int test_addn_pumped_through_buffer(void *state)         assert_sizet_equal(ctx.flushes, 2);         assert_sizet_equal(ctx.written, 2 * BUFFER_SIZE);         assert_true(memcmp(ctx.out, payload, 2 * BUFFER_SIZE) == 0); -       assert_sizet_equal(buf_len(&buf), PAYLOAD_SIZE - 2 * BUFFER_SIZE); -       assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE, -                          PAYLOAD_SIZE - 2 * BUFFER_SIZE) == 0); +       const int remains = PAYLOAD_SIZE - writes * BUFFER_SIZE; +       assert_sizet_equal(buf_len(&buf), remains); +       assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE, remains) == 0);         return TEST_EXIT_SUCCESS;  } > >> + assert_true(memcmp(mem, payload + 2 * BUFFER_SIZE, >> + PAYLOAD_SIZE - 2 * BUFFER_SIZE) == 0); >> + >> + return TEST_EXIT_SUCCESS; >> +} >> + >> +static int test_flush_on_overflow(void *state) >> +{ >> + uint8_t mem[BUFFER_SIZE]; >> + uint8_t payload[BUFFER_SIZE]; >> + struct lj_wbuf buf; >> + struct writer_ctx ctx; >> + >> + UNUSED(state); >> + >> + memset(payload, 'a', sizeof(payload)); >> + writer_ctx_init(&ctx, WMODE_OK); >> + lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem)); >> + >> + /* A byte written to a full buffer is preceded by a flush. */ >> + lj_wbuf_addn(&buf, payload, sizeof(payload)); >> + assert_sizet_equal(buf_len(&buf), sizeof(mem)); >> + lj_wbuf_addbyte(&buf, 0xaa); >> + assert_sizet_equal(ctx.flushes, 1); >> + assert_sizet_equal(ctx.written, sizeof(mem)); >> + assert_sizet_equal(buf_len(&buf), 1); >> + assert_int_equal(mem[0], 0xaa); > I suggest splitting this 1 test into 3 subtests, since they have the > similar logic: > 1) Init buffer. > 2) Write something without flushes. > 3) Write something with a flush. > > It helps to avoid the specific `ctx.out` size and makes checks for > `flushes`, `written` and `buf_len()` similar. Splitted: diff --git a/test/tarantool-c-tests/unit-wbuf.test.c b/test/tarantool-c-tests/unit-wbuf.test.c index 27d75ad0..9574d42b 100644 --- a/test/tarantool-c-tests/unit-wbuf.test.c +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -48,10 +48,10 @@ struct writer_ctx {       * Everything reported as written, in order of writing.       *       * Must hold the bytes reported over a whole test. The -     * largest amount is produced by test_flush_on_overflow(), -     * which reports 3 * BUFFER_SIZE bytes. 2 * PAYLOAD_SIZE -     * (4 * BUFFER_SIZE + 10) is a conservative upper bound -     * that leaves some headroom. +     * 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];  }; @@ -251,7 +251,7 @@ static int test_addn_pumped_through_buffer_partial(void *state)      return TEST_EXIT_SUCCESS;  } -static int test_flush_on_overflow(void *state) +static int test_flush_on_overflow_addbyte(void *state)  {      uint8_t payload[BUFFER_SIZE]; @@ -259,29 +259,57 @@ static int test_flush_on_overflow(void *state)      UNUSED(state);      memset(payload, 'a', sizeof(payload)); -    /* A byte written to a full buffer is preceded by a flush. */ +    /* 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); -    /* The same for an ULEB128 value written to a full buffer. */ -    lj_wbuf_addn(&buf, payload, sizeof(payload) - 1); +    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, 2); -    assert_sizet_equal(ctx.written, 2 * sizeof(mem)); +    assert_sizet_equal(ctx.flushes, 1); +    assert_sizet_equal(ctx.written, sizeof(mem));      assert_sizet_equal(wbuf_len(&buf), 1); -    /* And for a data block written to a full buffer. */ -    lj_wbuf_addn(&buf, payload, sizeof(payload) - 1); +    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, 3); -    assert_sizet_equal(ctx.written, 3 * sizeof(mem)); +    assert_sizet_equal(ctx.flushes, 1); +    assert_sizet_equal(ctx.written, sizeof(mem));      assert_sizet_equal(wbuf_len(&buf), 1);      return TEST_EXIT_SUCCESS; @@ -496,7 +524,9 @@ int main(void)          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), +        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),          test_unit_def(test_flush_partial_write),          test_unit_def(test_flush_stopped_stream), >> + >> + /* The same for an ULEB128 value written to a full buffer. */ >> + lj_wbuf_addn(&buf, payload, sizeof(payload) - 1); >> + assert_sizet_equal(buf_len(&buf), sizeof(mem)); >> + lj_wbuf_addu64(&buf, 0); >> + assert_sizet_equal(ctx.flushes, 2); >> + assert_sizet_equal(ctx.written, 2 * sizeof(mem)); >> + assert_sizet_equal(buf_len(&buf), 1); > Looks like there should be a check for the content as for the 0xaa > byte. Added: --- a/test/tarantool-c-tests/unit-wbuf.test.c +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -290,6 +290,8 @@ static int test_flush_on_overflow_addu64(void *state)         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;  } @@ -311,6 +313,7 @@ static int test_flush_on_overflow_addn(void *state)         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;  } >> + >> + /* And for a data block written to a full buffer. */ >> + lj_wbuf_addn(&buf, payload, sizeof(payload) - 1); >> + assert_sizet_equal(buf_len(&buf), sizeof(mem)); >> + lj_wbuf_addn(&buf, payload, 1); >> + assert_sizet_equal(ctx.flushes, 3); >> + assert_sizet_equal(ctx.written, 3 * sizeof(mem)); >> + assert_sizet_equal(buf_len(&buf), 1); > Looks like there should be a check for the content as for the 0xaa > byte. Added. >> + >> + return TEST_EXIT_SUCCESS; >> +} >> + >> +static int test_flush(void *state) > I suggest renaming this as `test_flush_and_write()` or something similar. > Otherwise, the difference between this test and the test above is > unclear. renamed: --- a/test/tarantool-c-tests/unit-wbuf.test.c +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -318,7 +318,7 @@ static int test_flush_on_overflow_addn(void *state)         return TEST_EXIT_SUCCESS;  } -static int test_flush(void *state) +static int test_flush_and_write(void *state)  {         WBUF_TEST_SETUP(WMODE_OK); @@ -530,7 +530,7 @@ int main(void)                 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), +               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), >> +{ >> + uint8_t mem[BUFFER_SIZE]; >> + struct lj_wbuf buf; >> + struct writer_ctx ctx; >> + >> + UNUSED(state); >> + >> + writer_ctx_init(&ctx, WMODE_OK); >> + lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem)); >> + >> + 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(buf_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(buf_len(&buf), 1); >> + assert_int_equal(mem[0], 0x7f); >> + >> + return TEST_EXIT_SUCCESS; >> +} >> + >> +static int test_flush_partial_write(void *state) >> +{ >> + uint8_t mem[BUFFER_SIZE]; >> + struct lj_wbuf buf; >> + struct writer_ctx ctx; >> + >> + UNUSED(state); >> + >> + writer_ctx_init(&ctx, WMODE_PARTIAL); >> + lj_wbuf_init(&buf, test_writer, &ctx, mem, sizeof(mem)); >> + >> + 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 unwritten data is dropped. */ >> + assert_sizet_equal(buf_len(&buf), 0); > I'm not sure that we should fixate this policy. Generally there is no > guarantee what has happened with data. Except that it is not written > successfully. > Feel free to ignore. Updated: --- a/test/tarantool-c-tests/unit-wbuf.test.c +++ b/test/tarantool-c-tests/unit-wbuf.test.c @@ -359,7 +359,7 @@ static int test_flush_partial_write(void *state)          */         assert_false(lj_wbuf_test_flag(&buf, STREAM_STOP));         assert_ptr_equal(buf.buf, mem); -       /* The unwritten data is dropped. */ +       /* The buffer is reset after the flush. */         assert_sizet_equal(wbuf_len(&buf), 0);         return TEST_EXIT_SUCCESS; > >> + >> + 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. >> +*/ > Minor: It is better to use > | /* > | * > | */ > > Comments here as in the most C/C++ code in the tests. > The inconsistent comment style in the is my fault > and should be fixed. Fixed: @@ -370,11 +372,11 @@ static int test_terminate(void *state)  }  /* -** 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. -*/ + * 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[] = { >> +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_flush_on_overflow), >> + test_unit_def(test_flush), >> + 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); > Minor: Why do we need this additional variable? I don't like when `return` returns an expression. I would leave as is. The same in leb128 test: $ grep test_run_group test/tarantool-c-tests/unit-*.c test/tarantool-c-tests/unit-leb128.test.c:      const int test_result = test_run_group(tgroup, NULL); test/tarantool-c-tests/unit-tap.test.c: return test_run_group(tgroup, NULL); test/tarantool-c-tests/unit-wbuf.test.c:        const int test_result = test_run_group(tgroup, NULL); > >> + >> + return test_result; >> +} >> -- >> 2.56.0 >>