Mercurial > dovecot > core-2.2
annotate src/lib-test/test-ostream.c @ 20810:5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Fri, 30 Sep 2016 13:42:45 +0300 |
parents | |
children | d2969430234a |
rev | line source |
---|---|
20810
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
1 /* Copyright (c) 2016 Dovecot authors, see the included COPYING file */ |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
2 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
3 #include "lib.h" |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
4 #include "buffer.h" |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
5 #include "ostream-private.h" |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
6 #include "test-common.h" |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
7 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
8 struct test_ostream { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
9 struct ostream_private ostream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
10 buffer_t *internal_buf; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
11 buffer_t *output_buf; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
12 size_t max_output_size; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
13 struct timeout *to; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
14 bool flush_pending; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
15 }; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
16 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
17 static void o_stream_test_destroy(struct iostream_private *stream) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
18 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
19 struct test_ostream *tstream = (struct test_ostream *)stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
20 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
21 if (tstream->to != NULL) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
22 timeout_remove(&tstream->to); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
23 if (tstream->internal_buf != NULL) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
24 i_free(tstream->internal_buf); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
25 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
26 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
27 static int o_stream_test_flush(struct ostream_private *stream) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
28 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
29 struct test_ostream *tstream = (struct test_ostream *)stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
30 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
31 if (tstream->internal_buf == NULL || tstream->internal_buf->used == 0) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
32 return 1; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
33 if (tstream->output_buf->used >= tstream->max_output_size) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
34 return 0; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
35 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
36 size_t left = tstream->max_output_size - tstream->output_buf->used; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
37 size_t n = I_MIN(left, tstream->internal_buf->used); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
38 buffer_append(tstream->output_buf, tstream->internal_buf->data, n); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
39 buffer_delete(tstream->internal_buf, 0, n); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
40 return tstream->internal_buf->used == 0 ? 1 : 0; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
41 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
42 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
43 static ssize_t |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
44 o_stream_test_sendv(struct ostream_private *stream, |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
45 const struct const_iovec *iov, unsigned int iov_count) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
46 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
47 struct test_ostream *tstream = (struct test_ostream *)stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
48 struct const_iovec cur_iov = { NULL, 0 }; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
49 size_t left, n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
50 ssize_t ret = 0; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
51 unsigned int i; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
52 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
53 /* first we need to try to flush the internal buffer */ |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
54 if ((ret = o_stream_test_flush(stream)) <= 0) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
55 return ret; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
56 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
57 /* append to output_buf until max_output_size is reached */ |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
58 ret = 0; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
59 for (i = 0; i < iov_count; i++) { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
60 left = tstream->max_output_size < tstream->output_buf->used ? 0 : |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
61 tstream->max_output_size - tstream->output_buf->used; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
62 n = I_MIN(left, iov[i].iov_len); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
63 buffer_append(tstream->output_buf, iov[i].iov_base, n); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
64 stream->ostream.offset += n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
65 ret += n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
66 if (n != iov[i].iov_len) { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
67 cur_iov.iov_base = CONST_PTR_OFFSET(iov[i].iov_base, n); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
68 cur_iov.iov_len = iov[i].iov_len - n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
69 break; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
70 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
71 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
72 /* if we've internal_buf, append to it until max_buffer_size is |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
73 reached */ |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
74 if (i == iov_count || tstream->internal_buf == NULL) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
75 return ret; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
76 do { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
77 left = tstream->ostream.max_buffer_size - |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
78 tstream->internal_buf->used; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
79 n = I_MIN(left, cur_iov.iov_len); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
80 buffer_append(tstream->internal_buf, cur_iov.iov_base, n); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
81 stream->ostream.offset += n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
82 ret += n; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
83 if (n != cur_iov.iov_len) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
84 break; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
85 cur_iov = iov[++i]; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
86 } while (i < iov_count); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
87 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
88 tstream->flush_pending = TRUE; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
89 return ret; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
90 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
91 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
92 static void test_ostream_send_more(struct test_ostream *tstream) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
93 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
94 struct ostream *ostream = &tstream->ostream.ostream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
95 int ret; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
96 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
97 o_stream_ref(ostream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
98 tstream->flush_pending = FALSE; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
99 if (tstream->ostream.callback != NULL) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
100 ret = tstream->ostream.callback(tstream->ostream.context); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
101 else |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
102 ret = o_stream_test_flush(&tstream->ostream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
103 if (ret == 0 || (tstream->internal_buf != NULL && |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
104 tstream->internal_buf->used > 0)) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
105 tstream->flush_pending = TRUE; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
106 if (!tstream->flush_pending || |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
107 tstream->output_buf->used >= tstream->max_output_size) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
108 timeout_remove(&tstream->to); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
109 o_stream_unref(&ostream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
110 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
111 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
112 static void test_ostream_set_send_more_timeout(struct test_ostream *tstream) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
113 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
114 if (tstream->to == NULL && tstream->flush_pending && |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
115 tstream->output_buf->used < tstream->max_output_size) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
116 tstream->to = timeout_add_short(0, test_ostream_send_more, tstream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
117 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
118 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
119 static void |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
120 o_stream_test_flush_pending(struct ostream_private *stream, bool set) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
121 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
122 struct test_ostream *tstream = (struct test_ostream *)stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
123 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
124 if (tstream->internal_buf != NULL && tstream->internal_buf->used > 0) { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
125 /* we have internal data, won't reset flush_pending */ |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
126 i_assert(tstream->flush_pending); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
127 } else { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
128 tstream->flush_pending = set; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
129 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
130 if (set) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
131 test_ostream_set_send_more_timeout(tstream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
132 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
133 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
134 static size_t |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
135 o_stream_test_get_used_size(const struct ostream_private *stream) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
136 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
137 struct test_ostream *tstream = (struct test_ostream *)stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
138 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
139 return tstream->internal_buf == NULL ? 0 : |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
140 tstream->internal_buf->used; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
141 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
142 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
143 struct ostream *test_ostream_create(buffer_t *output) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
144 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
145 struct test_ostream *tstream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
146 struct ostream *ostream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
147 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
148 tstream = i_new(struct test_ostream, 1); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
149 tstream->ostream.max_buffer_size = (size_t)-1; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
150 tstream->ostream.iostream.destroy = o_stream_test_destroy; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
151 tstream->ostream.sendv = o_stream_test_sendv; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
152 tstream->ostream.flush = o_stream_test_flush; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
153 tstream->ostream.flush_pending = o_stream_test_flush_pending; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
154 tstream->ostream.get_used_size = o_stream_test_get_used_size; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
155 tstream->ostream.ostream.blocking = TRUE; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
156 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
157 tstream->output_buf = output; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
158 tstream->max_output_size = (size_t)-1; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
159 ostream = o_stream_create(&tstream->ostream, NULL, -1); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
160 o_stream_set_name(ostream, "(test-ostream)"); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
161 return ostream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
162 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
163 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
164 struct ostream *test_ostream_create_nonblocking(buffer_t *output, |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
165 size_t max_internal_buffer_size) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
166 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
167 struct test_ostream *tstream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
168 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
169 tstream = (struct test_ostream *)test_ostream_create(output)->real_stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
170 tstream->internal_buf = buffer_create_dynamic(default_pool, 128); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
171 tstream->ostream.ostream.blocking = FALSE; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
172 tstream->ostream.max_buffer_size = max_internal_buffer_size; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
173 return &tstream->ostream.ostream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
174 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
175 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
176 static struct test_ostream *test_ostream_find(struct ostream *output) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
177 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
178 struct ostream *out; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
179 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
180 for (out = output; out != NULL; out = out->real_stream->parent) { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
181 if (out->real_stream->sendv == o_stream_test_sendv) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
182 return (struct test_ostream *)out->real_stream; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
183 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
184 i_panic("%s isn't test-ostream", o_stream_get_name(output)); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
185 } |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
186 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
187 void test_ostream_set_max_output_size(struct ostream *output, size_t max_size) |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
188 { |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
189 struct test_ostream *tstream = test_ostream_find(output); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
190 |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
191 tstream->max_output_size = max_size; |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
192 test_ostream_set_send_more_timeout(tstream); |
5856fd118fb3
lib-test: Added test_ostream for testing nonblocking ostreams.
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
diff
changeset
|
193 } |