2
2
3
3
use std:: sync:: Arc ;
4
4
5
- use futures:: executor:: block_on;
5
+ use futures:: executor:: ThreadPoolBuilder ;
6
+ use futures:: join;
6
7
use futures:: prelude:: * ;
7
8
use futures:: sink:: SinkExt ;
8
9
use grpcio:: {
@@ -11,6 +12,8 @@ use grpcio::{
11
12
} ;
12
13
use grpcio_proto:: example:: route_guide:: * ;
13
14
15
+ const MESSAGE_NUM : i32 = 3000 ;
16
+
14
17
#[ derive( Clone ) ]
15
18
struct RouteGuideService { }
16
19
@@ -38,11 +41,16 @@ impl RouteGuide for RouteGuideService {
38
41
) ;
39
42
current_num += 1 ;
40
43
summary. point_count += 1 ;
44
+ // Send a reply message after receiving a limited number of messages, which
45
+ // can be used to test the correctness under different buffer strategies.
46
+ if current_num >= MESSAGE_NUM {
47
+ break ;
48
+ }
41
49
}
42
50
resp. success ( summary) . await ?;
43
51
Ok ( ( ) )
44
52
}
45
- . map_err ( |_ : grpcio:: Error | panic ! ( "server got error" ) )
53
+ . map_err ( |e : grpcio:: Error | panic ! ( "server got error: {:?}" , e ) )
46
54
. map ( |_| ( ) ) ;
47
55
ctx. spawn ( f)
48
56
}
@@ -71,11 +79,13 @@ fn test_client_send_all() {
71
79
let ch = ChannelBuilder :: new ( env) . connect ( & format ! ( "127.0.0.1:{}" , port) ) ;
72
80
let client = RouteGuideClient :: new ( ch) ;
73
81
82
+ let pool = ThreadPoolBuilder :: new ( ) . pool_size ( 2 ) . create ( ) . unwrap ( ) ;
83
+
74
84
let exec_test_f = async move {
75
- // test for send all
85
+ // Test for send all disable batch
76
86
let ( mut sink, receiver) = client. record_route ( ) . unwrap ( ) ;
77
87
let mut send_data = vec ! [ ] ;
78
- for i in 0 ..3000 {
88
+ for i in 0 ..MESSAGE_NUM {
79
89
let mut p = Point :: default ( ) ;
80
90
p. set_longitude ( i) ;
81
91
send_data. push ( p) ;
@@ -84,13 +94,13 @@ fn test_client_send_all() {
84
94
sink. send_all ( & mut send_stream. map ( move |item| Ok ( ( item, WriteFlags :: default ( ) ) ) ) )
85
95
. await
86
96
. unwrap ( ) ;
87
- sink. close ( ) . await . unwrap ( ) ;
88
97
let summary = receiver. await . unwrap ( ) ;
89
- assert_eq ! ( summary. get_point_count( ) , 3000 ) ;
90
- // test for send all enable batch
98
+ assert_eq ! ( summary. get_point_count( ) , MESSAGE_NUM ) ;
99
+
100
+ // Test for send all enable batch
91
101
let ( mut sink, receiver) = client. record_route ( ) . unwrap ( ) ;
92
102
let mut send_data = vec ! [ ] ;
93
- for i in 0 ..3000 {
103
+ for i in 0 ..MESSAGE_NUM {
94
104
let mut p = Point :: default ( ) ;
95
105
p. set_longitude ( i) ;
96
106
send_data. push ( p) ;
@@ -100,13 +110,13 @@ fn test_client_send_all() {
100
110
sink. send_all ( & mut send_stream. map ( move |item| Ok ( ( item, WriteFlags :: default ( ) ) ) ) )
101
111
. await
102
112
. unwrap ( ) ;
103
- sink. close ( ) . await . unwrap ( ) ;
104
113
let summary = receiver. await . unwrap ( ) ;
105
- assert_eq ! ( summary. get_point_count( ) , 3000 ) ;
106
- // test for send all and buffer hint is true
114
+ assert_eq ! ( summary. get_point_count( ) , MESSAGE_NUM ) ;
115
+
116
+ // Test for send all and all buffer hints are true
107
117
let ( mut sink, receiver) = client. record_route ( ) . unwrap ( ) ;
108
118
let mut send_data = vec ! [ ] ;
109
- for i in 0 ..3000 {
119
+ for i in 0 ..MESSAGE_NUM {
110
120
let mut p = Point :: default ( ) ;
111
121
p. set_longitude ( i) ;
112
122
send_data. push ( p) ;
@@ -118,9 +128,21 @@ fn test_client_send_all() {
118
128
)
119
129
. await
120
130
. unwrap ( ) ;
121
- sink. close ( ) . await . unwrap ( ) ;
122
- let summary = receiver. await . unwrap ( ) ;
123
- assert_eq ! ( summary. get_point_count( ) , 3000 ) ;
131
+ // The following code is to test that when all msgs are set to be buffered, the msgs
132
+ // should be stored in the buffer until `sink.close()` is called.
133
+ let ( tx, rx) = std:: sync:: mpsc:: channel ( ) ;
134
+ let close_sink_task = async move {
135
+ rx. recv_timeout ( std:: time:: Duration :: from_secs ( 1 ) )
136
+ . unwrap_err ( ) ;
137
+ sink. close ( ) . await . unwrap ( ) ;
138
+ rx. recv_timeout ( std:: time:: Duration :: from_secs ( 1 ) ) . unwrap ( ) ;
139
+ } ;
140
+ let recv_msg_task = async move {
141
+ let summary = receiver. await . unwrap ( ) ;
142
+ tx. send ( ( ) ) . unwrap ( ) ;
143
+ assert_eq ! ( summary. get_point_count( ) , MESSAGE_NUM ) ;
144
+ } ;
145
+ join ! ( close_sink_task, recv_msg_task) ;
124
146
} ;
125
- block_on ( exec_test_f) ;
147
+ pool . spawn_ok ( exec_test_f) ;
126
148
}
0 commit comments