Mercurial > dovecot > core-2.2
annotate src/lib/test-istream-tee.c @ 22664:fea53c2725c0
director: Fix director_max_parallel_moves/kicks type
Should be uint, not time.
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 09 Nov 2017 12:24:16 +0200 |
parents | 2e2563132d5f |
children | cb108f786fb4 |
rev | line source |
---|---|
21390
2e2563132d5f
Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
19552
diff
changeset
|
1 /* Copyright (c) 2009-2017 Dovecot authors, see the included COPYING file */ |
9786 | 2 |
3 #include "test-lib.h" | |
4 #include "str.h" | |
13529
cf77e448295c
Renamed lib/*-internal.h files to lib/*-private.h for consistency.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
5 #include "istream-private.h" |
9786 | 6 #include "istream-tee.h" |
7 | |
8 | |
9 #define TEST_BUF_SIZE I_STREAM_MIN_SIZE | |
10 #define TEST_STR_LEN (TEST_BUF_SIZE*3) | |
11 #define CHILD_COUNT 5 | |
12 | |
13 static void test_istream_tee_tailing(const char *str) | |
14 { | |
15 struct istream *test_input, *child_input[CHILD_COUNT]; | |
16 struct tee_istream *tee; | |
17617
f87c9ca897a5
lib: test-istream-tee - verify _read returns correct values after _set_size()
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
17 unsigned int i, len, delta; |
9786 | 18 |
19 test_input = test_istream_create(str); | |
20 test_istream_set_max_buffer_size(test_input, TEST_BUF_SIZE); | |
21 | |
22 test_begin("istream tee tailing"); | |
23 tee = tee_i_stream_create(test_input); | |
24 for (i = 0; i < CHILD_COUNT; i++) | |
25 child_input[i] = tee_i_stream_create_child(tee); | |
26 | |
27 test_istream_set_allow_eof(test_input, FALSE); | |
17617
f87c9ca897a5
lib: test-istream-tee - verify _read returns correct values after _set_size()
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
28 delta = 1; |
f87c9ca897a5
lib: test-istream-tee - verify _read returns correct values after _set_size()
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
29 for (len = 1; len < TEST_BUF_SIZE; len += delta) { |
9786 | 30 test_istream_set_size(test_input, len); |
31 for (i = 0; i < CHILD_COUNT; i++) { | |
17620
a366ae9b182f
lib: Compiler warning fixes for 32bit systems
Timo Sirainen <tss@iki.fi>
parents:
17619
diff
changeset
|
32 test_assert_idx(i_stream_read(child_input[i]) == (int)delta, len); |
17617
f87c9ca897a5
lib: test-istream-tee - verify _read returns correct values after _set_size()
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
33 test_assert_idx(!tee_i_stream_child_is_waiting(child_input[i]), len); |
f87c9ca897a5
lib: test-istream-tee - verify _read returns correct values after _set_size()
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
34 test_assert_idx(i_stream_read(child_input[i]) == 0, len); |
f87c9ca897a5
lib: test-istream-tee - verify _read returns correct values after _set_size()
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
35 test_assert_idx(!tee_i_stream_child_is_waiting(child_input[i]), len); |
9786 | 36 } |
17617
f87c9ca897a5
lib: test-istream-tee - verify _read returns correct values after _set_size()
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
37 delta = rand() % 32; /* may stand still */ |
f87c9ca897a5
lib: test-istream-tee - verify _read returns correct values after _set_size()
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
38 if(delta > TEST_BUF_SIZE - len) |
f87c9ca897a5
lib: test-istream-tee - verify _read returns correct values after _set_size()
Phil Carmody <phil@dovecot.fi>
parents:
17130
diff
changeset
|
39 delta = 1; |
9786 | 40 } |
41 | |
42 test_istream_set_size(test_input, len); | |
43 for (i = 0; i < CHILD_COUNT; i++) { | |
17620
a366ae9b182f
lib: Compiler warning fixes for 32bit systems
Timo Sirainen <tss@iki.fi>
parents:
17619
diff
changeset
|
44 test_assert(i_stream_read(child_input[i]) == (int)delta); |
9786 | 45 test_assert(i_stream_read(child_input[i]) == -2); |
10392
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
46 test_assert(!tee_i_stream_child_is_waiting(child_input[i])); |
9786 | 47 } |
48 | |
17618
cba6877471ca
lib: test-istream-tee - more randomisation to the tests
Phil Carmody <phil@dovecot.fi>
parents:
17617
diff
changeset
|
49 delta = 1; |
cba6877471ca
lib: test-istream-tee - more randomisation to the tests
Phil Carmody <phil@dovecot.fi>
parents:
17617
diff
changeset
|
50 while ((len += delta) <= TEST_STR_LEN) { |
17619
4f56f24b5bb2
lib: test-istream-tee - randomise which tee stream lags behind the others
Phil Carmody <phil@dovecot.fi>
parents:
17618
diff
changeset
|
51 unsigned int lagger = rand() % CHILD_COUNT; |
9786 | 52 test_istream_set_size(test_input, len); |
10392
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
53 for (i = 0; i < CHILD_COUNT; i++) { |
9786 | 54 test_assert(i_stream_read(child_input[i]) == -2); |
10392
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
55 test_assert(!tee_i_stream_child_is_waiting(child_input[i])); |
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
56 } |
17619
4f56f24b5bb2
lib: test-istream-tee - randomise which tee stream lags behind the others
Phil Carmody <phil@dovecot.fi>
parents:
17618
diff
changeset
|
57 for (i = 0; i < CHILD_COUNT; i++) { |
4f56f24b5bb2
lib: test-istream-tee - randomise which tee stream lags behind the others
Phil Carmody <phil@dovecot.fi>
parents:
17618
diff
changeset
|
58 if (i == lagger) |
4f56f24b5bb2
lib: test-istream-tee - randomise which tee stream lags behind the others
Phil Carmody <phil@dovecot.fi>
parents:
17618
diff
changeset
|
59 continue; |
17618
cba6877471ca
lib: test-istream-tee - more randomisation to the tests
Phil Carmody <phil@dovecot.fi>
parents:
17617
diff
changeset
|
60 i_stream_skip(child_input[i], delta); |
9786 | 61 test_assert(i_stream_read(child_input[i]) == 0); |
10392
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
62 test_assert(tee_i_stream_child_is_waiting(child_input[i])); |
9786 | 63 } |
17619
4f56f24b5bb2
lib: test-istream-tee - randomise which tee stream lags behind the others
Phil Carmody <phil@dovecot.fi>
parents:
17618
diff
changeset
|
64 i_stream_skip(child_input[lagger], delta); |
9786 | 65 for (i = 0; i < CHILD_COUNT; i++) { |
17620
a366ae9b182f
lib: Compiler warning fixes for 32bit systems
Timo Sirainen <tss@iki.fi>
parents:
17619
diff
changeset
|
66 test_assert(i_stream_read(child_input[i]) == (int)delta); |
9786 | 67 test_assert(i_stream_read(child_input[i]) == -2); |
10392
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
68 test_assert(!tee_i_stream_child_is_waiting(child_input[i])); |
9786 | 69 } |
17618
cba6877471ca
lib: test-istream-tee - more randomisation to the tests
Phil Carmody <phil@dovecot.fi>
parents:
17617
diff
changeset
|
70 delta = rand() % 31 + 1; /* mustn't stand still */ |
cba6877471ca
lib: test-istream-tee - more randomisation to the tests
Phil Carmody <phil@dovecot.fi>
parents:
17617
diff
changeset
|
71 if(delta > TEST_STR_LEN - len) |
cba6877471ca
lib: test-istream-tee - more randomisation to the tests
Phil Carmody <phil@dovecot.fi>
parents:
17617
diff
changeset
|
72 delta = 1; |
9786 | 73 } |
74 | |
10392
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
75 for (i = 0; i < CHILD_COUNT-1; i++) { |
9786 | 76 i_stream_skip(child_input[i], 1); |
77 test_assert(i_stream_read(child_input[i]) == 0); | |
10392
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
78 test_assert(tee_i_stream_child_is_waiting(child_input[i])); |
9786 | 79 } |
10392
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
80 i_stream_skip(child_input[i], 1); |
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
81 test_assert(i_stream_read(child_input[i]) == 0); |
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
82 test_assert(!tee_i_stream_child_is_waiting(child_input[i])); |
9d878b1dc028
Added tee_i_stream_child_is_waiting().
Timo Sirainen <tss@iki.fi>
parents:
9786
diff
changeset
|
83 |
9786 | 84 test_istream_set_allow_eof(test_input, TRUE); |
85 for (i = 0; i < CHILD_COUNT; i++) { | |
86 test_assert(i_stream_read(child_input[i]) == -1); | |
87 i_stream_unref(&child_input[i]); | |
88 } | |
89 i_stream_unref(&test_input); | |
90 | |
91 test_end(); | |
92 } | |
93 | |
94 static void test_istream_tee_blocks(const char *str) | |
95 { | |
96 struct istream *test_input, *child_input[CHILD_COUNT]; | |
97 struct tee_istream *tee; | |
98 unsigned int i, j; | |
99 | |
100 test_input = test_istream_create(str); | |
101 test_istream_set_max_buffer_size(test_input, TEST_BUF_SIZE); | |
102 | |
103 test_begin("istream tee blocks"); | |
104 tee = tee_i_stream_create(test_input); | |
105 for (i = 0; i < CHILD_COUNT; i++) | |
106 child_input[i] = tee_i_stream_create_child(tee); | |
107 | |
108 test_istream_set_allow_eof(test_input, FALSE); | |
109 for (j = 1; j <= 3; j++) { | |
110 test_istream_set_size(test_input, TEST_BUF_SIZE*j); | |
111 for (i = 0; i < CHILD_COUNT; i++) { | |
112 test_assert(i_stream_read(child_input[i]) == TEST_BUF_SIZE); | |
113 i_stream_skip(child_input[i], TEST_BUF_SIZE); | |
114 } | |
115 } | |
116 test_istream_set_allow_eof(test_input, TRUE); | |
117 for (i = 0; i < CHILD_COUNT; i++) { | |
118 test_assert(i_stream_read(child_input[i]) == -1); | |
119 i_stream_unref(&child_input[i]); | |
120 } | |
121 i_stream_unref(&test_input); | |
122 | |
123 test_end(); | |
124 } | |
125 | |
126 void test_istream_tee(void) | |
127 { | |
128 string_t *str; | |
129 unsigned int i; | |
130 | |
131 str = str_new(default_pool, TEST_STR_LEN); | |
132 for (i = 0; i < TEST_STR_LEN; i++) | |
133 str_append_c(str, 'a' + i%26); | |
134 | |
135 test_istream_tee_tailing(str_c(str)); | |
136 test_istream_tee_blocks(str_c(str)); | |
137 | |
138 str_free(&str); | |
139 } |