Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests on OSX. #27

Merged
merged 4 commits into from
Sep 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ authors = [
"ShuYu Wang <andelf@gmail.com>",
"Jimmy Lu <gongchuo.lu@gmail.com>",
"Francisco Giordano <frangio.1@gmail.com>",
"Jake Kerr"
]

description = "Cross-platform filesystem notification library"
Expand Down
4 changes: 2 additions & 2 deletions src/fsevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ impl Watcher for FsEventWatcher {
fsevent = FsEventWatcher {
paths: cf::CFArrayCreateMutable(cf::kCFAllocatorDefault, 0, &cf::kCFTypeArrayCallBacks),
since_when: fs::kFSEventStreamEventIdSinceNow,
latency: 0.1,
flags: fs::kFSEventStreamCreateFlagFileEvents,
latency: 0.0,
flags: fs::kFSEventStreamCreateFlagFileEvents | fs::kFSEventStreamCreateFlagNoDefer,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, can you explain what that change does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defer will wait several event to aggregate them and deliver to the callback. This can mess tests (at least) and this seems to be a reasonable default, until somebody tells us this messes things. Same goes with latency.

sender: tx,
runloop: Arc::new(RwLock::new(None)),
context: None,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod op {
}
}

#[derive(Debug)]
pub struct Event {
pub path: Option<PathBuf>,
pub op: Result<Op, Error>,
Expand Down
87 changes: 76 additions & 11 deletions tests/notify.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,81 @@
extern crate notify;
extern crate tempdir;
extern crate tempfile;
extern crate time;


use notify::*;
use std::io::Write;
use std::path::Path;
#[cfg(target_os="macos")]
use std::path::Component;
#[cfg(target_os="macos")]
use std::fs::read_link;
use std::path::{Path, PathBuf};
use std::thread;
use std::sync::mpsc::{channel, Sender, Receiver};
use tempdir::TempDir;
use tempfile::NamedTempFile;

const TIMEOUT_S: f64 = 5.0;

// TODO: replace with std::fs::canonicalize rust-lang/rust#27706.
// OSX needs to resolve symlinks
#[cfg(target_os="macos")]
fn resolve_path(path: &Path) -> PathBuf {
let p = path.to_str().unwrap();

let mut out = PathBuf::new();
let buf = PathBuf::from(p);
for p in buf.components() {
match p {
Component::RootDir => out.push("/"),
Component::Normal(osstr) => {
out.push(osstr);
if let Ok(real) = read_link(&out) {
if real.is_relative() {
out.pop();
out.push(real);
} else {
out = real;
}
}
}
_ => ()
}
}
out
}


#[cfg(not(any(target_os="macos")))]
fn resolve_path(path: &Path) -> PathBuf {
let p = path.to_str().unwrap();
PathBuf::from(p)
}

fn validate_recv(rx: Receiver<Event>, evs: Vec<(&Path, Op)>) {
for expected in evs {
let actual = rx.recv().unwrap();
assert_eq!(actual.path.unwrap().as_path(), expected.0);
assert_eq!(actual.op.unwrap(), expected.1);
}
let deadline = time::precise_time_s() + TIMEOUT_S;
let mut evs = evs.clone();

assert!(rx.try_recv().is_err());
while time::precise_time_s() < deadline {
if let Ok(actual) = rx.try_recv() {
let path = actual.path.clone().unwrap();
let op = actual.op.unwrap().clone();
let mut removables = vec!();
for i in (0..evs.len()) {
let expected = evs.get(i).unwrap();
if path.clone().as_path() == expected.0 && op.contains(expected.1) {
removables.push(i);
}
}
for removable in removables {
evs.remove(removable);
}
}
if evs.is_empty() { break; }
}
assert!(evs.is_empty(),
"Some expected events did not occur before the test timedout:\n\t\t{:?}", evs);
}

fn validate_watch_single_file<F, W>(ctor: F) where
Expand All @@ -29,26 +87,32 @@ fn validate_watch_single_file<F, W>(ctor: F) where
thread::sleep_ms(1000);
file.write_all(b"foo").unwrap();
file.flush().unwrap();
validate_recv(rx, vec![(file.path(), op::WRITE)]);
validate_recv(rx, vec![(resolve_path(file.path()).as_path(), op::CREATE)]);
}


fn validate_watch_dir<F, W>(ctor: F) where
F: Fn(Sender<Event>) -> Result<W, Error>, W: Watcher {
let dir = TempDir::new("dir").unwrap();
let dir1 = TempDir::new_in(dir.path(), "dir1").unwrap();
let dir2 = TempDir::new_in(dir.path(), "dir2").unwrap();
let dir11 = TempDir::new_in(dir1.path(), "dir11").unwrap();
let (tx, rx) = channel();
// OSX FsEvent needs some time to discard old events from its log.
thread::sleep_ms(12000);
let mut w = ctor(tx).unwrap();

w.watch(dir.path()).unwrap();
let f111 = NamedTempFile::new_in(dir11.path()).unwrap();
let f111_path = f111.path().to_owned();
let f111_path = f111_path.as_path();
let f21 = NamedTempFile::new_in(dir2.path()).unwrap();
thread::sleep_ms(4000);
f111.close().unwrap();
validate_recv(rx, vec![(f111_path, op::CREATE),
(f21.path(), op::CREATE),
(f111_path, op::REMOVE)]);
thread::sleep_ms(4000);
validate_recv(rx, vec![(resolve_path(f111_path).as_path(), op::CREATE),
(resolve_path(f21.path()).as_path(), op::CREATE),
(resolve_path(f111_path).as_path(), op::REMOVE)]);
}

#[test]
Expand All @@ -63,6 +127,7 @@ fn watch_dir_recommended() {

#[test]
fn watch_single_file_poll() {
// Currently broken on OSX because relative filename are sent.
validate_watch_single_file(PollWatcher::new);
}

Expand Down