@@ -145,121 +145,58 @@ impl Drop for HomingMissile {
145
145
146
146
#[ cfg( test) ]
147
147
mod test {
148
+ use green:: sched;
149
+ use green:: { SchedPool , PoolConfig } ;
150
+ use std:: rt:: rtio:: RtioUdpSocket ;
151
+ use std:: io:: test:: next_test_ip4;
152
+ use std:: task:: TaskOpts ;
153
+
154
+ use net:: UdpWatcher ;
155
+ use super :: super :: local_loop;
156
+
148
157
// On one thread, create a udp socket. Then send that socket to another
149
158
// thread and destroy the socket on the remote thread. This should make sure
150
159
// that homing kicks in for the socket to go back home to the original
151
160
// thread, close itself, and then come back to the last thread.
152
- //#[test]
153
- //fn test_homing_closes_correctly() {
154
- // let (port, chan) = Chan::new();
155
-
156
- // do task::spawn_sched(task::SingleThreaded) {
157
- // let listener = UdpWatcher::bind(local_loop(), next_test_ip4()).unwrap();
158
- // chan.send(listener);
159
- // }
160
-
161
- // do task::spawn_sched(task::SingleThreaded) {
162
- // port.recv();
163
- // }
164
- //}
165
-
166
- // This is a bit of a crufty old test, but it has its uses.
167
- //#[test]
168
- //fn test_simple_homed_udp_io_bind_then_move_task_then_home_and_close() {
169
- // use std::cast;
170
- // use std::rt::local::Local;
171
- // use std::rt::rtio::{EventLoop, IoFactory};
172
- // use std::rt::sched::Scheduler;
173
- // use std::rt::sched::{Shutdown, TaskFromFriend};
174
- // use std::rt::sleeper_list::SleeperList;
175
- // use std::rt::task::Task;
176
- // use std::rt::task::UnwindResult;
177
- // use std::rt::thread::Thread;
178
- // use std::rt::deque::BufferPool;
179
- // use std::unstable::run_in_bare_thread;
180
- // use uvio::UvEventLoop;
181
-
182
- // do run_in_bare_thread {
183
- // let sleepers = SleeperList::new();
184
- // let mut pool = BufferPool::new();
185
- // let (worker1, stealer1) = pool.deque();
186
- // let (worker2, stealer2) = pool.deque();
187
- // let queues = ~[stealer1, stealer2];
188
-
189
- // let loop1 = ~UvEventLoop::new() as ~EventLoop;
190
- // let mut sched1 = ~Scheduler::new(loop1, worker1, queues.clone(),
191
- // sleepers.clone());
192
- // let loop2 = ~UvEventLoop::new() as ~EventLoop;
193
- // let mut sched2 = ~Scheduler::new(loop2, worker2, queues.clone(),
194
- // sleepers.clone());
195
-
196
- // let handle1 = sched1.make_handle();
197
- // let handle2 = sched2.make_handle();
198
- // let tasksFriendHandle = sched2.make_handle();
199
-
200
- // let on_exit: proc(UnwindResult) = proc(exit_status) {
201
- // let mut handle1 = handle1;
202
- // let mut handle2 = handle2;
203
- // handle1.send(Shutdown);
204
- // handle2.send(Shutdown);
205
- // assert!(exit_status.is_success());
206
- // };
207
-
208
- // unsafe fn local_io() -> &'static mut IoFactory {
209
- // let mut sched = Local::borrow(None::<Scheduler>);
210
- // let io = sched.get().event_loop.io();
211
- // cast::transmute(io.unwrap())
212
- // }
213
-
214
- // let test_function: proc() = proc() {
215
- // let io = unsafe { local_io() };
216
- // let addr = next_test_ip4();
217
- // let maybe_socket = io.udp_bind(addr);
218
- // // this socket is bound to this event loop
219
- // assert!(maybe_socket.is_ok());
220
-
221
- // // block self on sched1
222
- // let scheduler: ~Scheduler = Local::take();
223
- // let mut tasksFriendHandle = Some(tasksFriendHandle);
224
- // scheduler.deschedule_running_task_and_then(|_, task| {
225
- // // unblock task
226
- // task.wake().map(|task| {
227
- // // send self to sched2
228
- // tasksFriendHandle.take_unwrap()
229
- // .send(TaskFromFriend(task));
230
- // });
231
- // // sched1 should now sleep since it has nothing else to do
232
- // })
233
- // // sched2 will wake up and get the task as we do nothing else,
234
- // // the function ends and the socket goes out of scope sched2
235
- // // will start to run the destructor the destructor will first
236
- // // block the task, set it's home as sched1, then enqueue it
237
- // // sched2 will dequeue the task, see that it has a home, and
238
- // // send it to sched1 sched1 will wake up, exec the close
239
- // // function on the correct loop, and then we're done
240
- // };
161
+ #[ test]
162
+ fn test_homing_closes_correctly ( ) {
163
+ let ( port, chan) = Chan :: new ( ) ;
164
+ let mut pool = SchedPool :: new ( PoolConfig { threads : 1 } ) ;
165
+
166
+ do pool. spawn ( TaskOpts :: new ( ) ) {
167
+ let listener = UdpWatcher :: bind ( local_loop ( ) , next_test_ip4 ( ) ) ;
168
+ chan. send ( listener. unwrap ( ) ) ;
169
+ }
241
170
242
- // let mut main_task = ~Task::new_root(&mut sched1.stack_pool, None,
243
- // test_function);
244
- // main_task.death.on_exit = Some(on_exit);
171
+ let task = do pool. task ( TaskOpts :: new ( ) ) {
172
+ port. recv ( ) ;
173
+ } ;
174
+ pool. spawn_sched ( ) . send ( sched:: TaskFromFriend ( task) ) ;
245
175
246
- // let null_task = ~do Task::new_root(&mut sched2.stack_pool, None) {
247
- // // nothing
248
- // };
176
+ pool. shutdown ( ) ;
177
+ }
249
178
250
- // let main_task = main_task;
251
- // let sched1 = sched1;
252
- // let thread1 = do Thread::start {
253
- // sched1.bootstrap(main_task);
254
- // };
179
+ #[ test]
180
+ fn test_homing_read ( ) {
181
+ let ( port, chan) = Chan :: new ( ) ;
182
+ let mut pool = SchedPool :: new ( PoolConfig { threads : 1 } ) ;
183
+
184
+ do pool. spawn ( TaskOpts :: new ( ) ) {
185
+ let addr1 = next_test_ip4 ( ) ;
186
+ let addr2 = next_test_ip4 ( ) ;
187
+ let listener = UdpWatcher :: bind ( local_loop ( ) , addr2) ;
188
+ chan. send ( ( listener. unwrap ( ) , addr1) ) ;
189
+ let mut listener = UdpWatcher :: bind ( local_loop ( ) , addr1) . unwrap ( ) ;
190
+ listener. sendto ( [ 1 , 2 , 3 , 4 ] , addr2) ;
191
+ }
255
192
256
- // let sched2 = sched2;
257
- // let thread2 = do Thread::start {
258
- // sched2.bootstrap(null_task);
259
- // };
193
+ let task = do pool. task ( TaskOpts :: new ( ) ) {
194
+ let ( mut watcher, addr) = port. recv ( ) ;
195
+ let mut buf = [ 0 , ..10 ] ;
196
+ assert_eq ! ( watcher. recvfrom( buf) . unwrap( ) , ( 4 , addr) ) ;
197
+ } ;
198
+ pool. spawn_sched ( ) . send ( sched:: TaskFromFriend ( task) ) ;
260
199
261
- // thread1.join();
262
- // thread2.join();
263
- // }
264
- //}
200
+ pool. shutdown ( ) ;
201
+ }
265
202
}
0 commit comments