|
| 1 | +// This file is part of Grust, GObject introspection bindings for Rust |
| 2 | +// |
| 3 | +// Copyright (C) 2015 Mikhail Zabaluev <[email protected]> |
| 4 | +// |
| 5 | +// This library is free software; you can redistribute it and/or |
| 6 | +// modify it under the terms of the GNU Lesser General Public |
| 7 | +// License as published by the Free Software Foundation; either |
| 8 | +// version 2.1 of the License, or (at your option) any later version. |
| 9 | +// |
| 10 | +// This library is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | +// Lesser General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Lesser General Public |
| 16 | +// License along with this library; if not, write to the Free Software |
| 17 | +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + |
| 19 | +extern crate grust; |
| 20 | + |
| 21 | +use grust::mainloop; |
| 22 | +use grust::mainloop::{LoopRunner, Source, SourceCallback}; |
| 23 | +use grust::mainloop::CallbackResult::{Continue, Remove}; |
| 24 | + |
| 25 | +use std::sync::mpsc; |
| 26 | +use std::thread; |
| 27 | + |
| 28 | +#[test] |
| 29 | +fn test_invoke_once() { |
| 30 | + let runner = LoopRunner::new(); |
| 31 | + runner.run_after(|mainloop| { |
| 32 | + const THREAD_NAME: &'static str = "invoker"; |
| 33 | + thread::Builder::new().name(THREAD_NAME.to_string()).spawn(move || { |
| 34 | + let mlc = mainloop.clone(); |
| 35 | + let ctx = mainloop.get_context(); |
| 36 | + ctx.invoke(SourceCallback::once(move || { |
| 37 | + assert!(thread::current().name() != Some(THREAD_NAME)); |
| 38 | + mlc.quit(); |
| 39 | + })); |
| 40 | + }).unwrap(); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +fn test_invoke() { |
| 46 | + let runner = LoopRunner::new(); |
| 47 | + runner.run_after(|mainloop| { |
| 48 | + const THREAD_NAME: &'static str = "invoker"; |
| 49 | + thread::Builder::new().name(THREAD_NAME.to_string()).spawn(move || { |
| 50 | + let mlc = mainloop.clone(); |
| 51 | + let mut count = 0; |
| 52 | + let ctx = mainloop.get_context(); |
| 53 | + ctx.invoke(SourceCallback::new(move || { |
| 54 | + assert!(thread::current().name() != Some(THREAD_NAME)); |
| 55 | + count += 1; |
| 56 | + if count < 2 { |
| 57 | + Continue |
| 58 | + } else { |
| 59 | + mlc.quit(); |
| 60 | + Remove |
| 61 | + } |
| 62 | + })); |
| 63 | + }).unwrap(); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +#[test] |
| 68 | +fn test_idle_source() { |
| 69 | + let runner = LoopRunner::new(); |
| 70 | + runner.run_after(|ml| { |
| 71 | + let source = mainloop::idle_source_new(); |
| 72 | + let mlc = ml.clone(); |
| 73 | + let mut count = 0; |
| 74 | + source.set_callback(SourceCallback::new(move || { |
| 75 | + assert!(count <= 2); |
| 76 | + count += 1; |
| 77 | + if count < 2 { |
| 78 | + Continue |
| 79 | + } else { |
| 80 | + mlc.quit(); |
| 81 | + Remove |
| 82 | + } |
| 83 | + })); |
| 84 | + source.attach(ml.get_context()); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +#[test] |
| 89 | +fn test_one_time_callback() { |
| 90 | + let runner = LoopRunner::new(); |
| 91 | + runner.run_after(|ml| { |
| 92 | + let source = mainloop::idle_source_new(); |
| 93 | + let mlc = ml.clone(); |
| 94 | + source.set_callback(SourceCallback::once(move || { |
| 95 | + mlc.quit(); |
| 96 | + })); |
| 97 | + source.attach(ml.get_context()); |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +#[test] |
| 102 | +fn test_timeout_source() { |
| 103 | + let runner = LoopRunner::new(); |
| 104 | + runner.run_after(|ml| { |
| 105 | + let source = mainloop::timeout_source_new(10); |
| 106 | + let mlc = ml.clone(); |
| 107 | + source.set_callback(SourceCallback::once(move || { |
| 108 | + mlc.quit(); |
| 109 | + })); |
| 110 | + source.attach(ml.get_context()); |
| 111 | + }); |
| 112 | +} |
| 113 | + |
| 114 | +#[test] |
| 115 | +fn test_priority() { |
| 116 | + let (tx, rx) = mpsc::channel(); |
| 117 | + let runner = LoopRunner::new(); |
| 118 | + runner.run_after(|ml| { |
| 119 | + let source1 = mainloop::idle_source_new(); |
| 120 | + source1.set_priority(mainloop::PRIORITY_DEFAULT); |
| 121 | + let mut count = 0; |
| 122 | + source1.set_callback(SourceCallback::new(move || { |
| 123 | + tx.send(()).unwrap(); |
| 124 | + count += 1; |
| 125 | + if count == 1 { |
| 126 | + Remove |
| 127 | + } else { |
| 128 | + Continue |
| 129 | + } |
| 130 | + })); |
| 131 | + let source2 = mainloop::idle_source_new(); |
| 132 | + let mlc = ml.clone(); |
| 133 | + source2.set_callback(SourceCallback::once(move || { |
| 134 | + mlc.quit(); |
| 135 | + })); |
| 136 | + let ctx = ml.get_context(); |
| 137 | + source1.attach(ctx); |
| 138 | + source2.attach(ctx); |
| 139 | + }); |
| 140 | + assert_eq!(rx.iter().count(), 1); |
| 141 | +} |
| 142 | + |
| 143 | +#[test] |
| 144 | +fn test_attached_source() { |
| 145 | + let (tx, rx) = mpsc::channel(); |
| 146 | + let runner = LoopRunner::new(); |
| 147 | + runner.run_after(|ml| { |
| 148 | + let ctx = ml.get_context(); |
| 149 | + let source1 = mainloop::idle_source_new(); |
| 150 | + let attached = source1.attach(ctx); |
| 151 | + let attached_source: &Source = attached.as_ref(); |
| 152 | + attached_source.set_priority(mainloop::PRIORITY_DEFAULT); |
| 153 | + let atc = attached.clone(); |
| 154 | + attached.as_source().set_callback(SourceCallback::new(move || { |
| 155 | + tx.send(()).unwrap(); |
| 156 | + atc.destroy(); |
| 157 | + Continue |
| 158 | + })); |
| 159 | + let mlc = ml.clone(); |
| 160 | + let source2 = mainloop::idle_source_new(); |
| 161 | + source2.set_callback(SourceCallback::once(move || { |
| 162 | + mlc.quit(); |
| 163 | + })); |
| 164 | + source2.attach(ctx); |
| 165 | + }); |
| 166 | + assert_eq!(rx.iter().count(), 1); |
| 167 | +} |
0 commit comments