From db78d6a7e5fa5c8592b4064617937ad700ec4e32 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Wed, 28 Nov 2012 17:16:15 -0700 Subject: [PATCH] Ensure PathWatcher isn't watching anything at the end of each spec This replaces the old functionality of ensuring no files or directories have subscriptions in javascript. We allow this now, but we just don't allow leaked watches at the native layer. --- native/path_watcher.h | 1 + native/path_watcher.mm | 4 ++++ native/v8_extensions/native.js | 3 +++ native/v8_extensions/native.mm | 15 +++++++++++++++ spec/spec-helper.coffee | 24 +++++------------------- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/native/path_watcher.h b/native/path_watcher.h index 2e69f785f..8681b6e38 100644 --- a/native/path_watcher.h +++ b/native/path_watcher.h @@ -19,5 +19,6 @@ typedef void (^WatchCallback)(NSString *, NSString *); - (id)initWithContext:(CefRefPtr)context; - (NSString *)watchPath:(NSString *)path callback:(WatchCallback)callback; - (void)unwatchPath:(NSString *)path callbackId:(NSString *)callbackId error:(NSError **)error; +- (NSArray *)watchedPaths; @end diff --git a/native/path_watcher.mm b/native/path_watcher.mm index 2bd7f1ae8..2e2cf497f 100644 --- a/native/path_watcher.mm +++ b/native/path_watcher.mm @@ -144,6 +144,10 @@ static NSMutableArray *gPathWatchers; } } +- (NSArray *)watchedPaths { + return [_callbacksByPath allKeys]; +} + - (bool)createKeventForPath:(NSString *)path { path = [path stringByStandardizingPath]; diff --git a/native/v8_extensions/native.js b/native/v8_extensions/native.js index 3f087c5ee..02a434795 100644 --- a/native/v8_extensions/native.js +++ b/native/v8_extensions/native.js @@ -43,6 +43,9 @@ var $native = {}; native function unwatchPath(path, callbackId); $native.unwatchPath = unwatchPath; + native function getWatchedPaths(); + $native.getWatchedPaths = getWatchedPaths; + native function makeDirectory(path); $native.makeDirectory = makeDirectory; diff --git a/native/v8_extensions/native.mm b/native/v8_extensions/native.mm index 73b87bb06..b2c9f1482 100644 --- a/native/v8_extensions/native.mm +++ b/native/v8_extensions/native.mm @@ -237,6 +237,21 @@ bool Native::Execute(const CefString& name, return true; } + + else if (name == "getWatchedPaths") { + PathWatcher *pathWatcher = [PathWatcher pathWatcherForContext:CefV8Context::GetCurrentContext()]; + NSArray *paths = [pathWatcher watchedPaths]; + + CefRefPtr pathsArray = CefV8Value::CreateArray([paths count]); + + for (int i = 0; i < [paths count]; i++) { + CefRefPtr path = CefV8Value::CreateString([[paths objectAtIndex:i] UTF8String]); + pathsArray->SetValue(i, path); + } + retval = pathsArray; + + return true; + } else if (name == "makeDirectory") { NSString *path = stringFromCefV8Value(arguments[0]); NSFileManager *fm = [NSFileManager defaultManager]; diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 9cb752542..10671aafe 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -16,13 +16,11 @@ require 'window' requireStylesheet "jasmine.css" -pathsWithSubscriptions = null jasmine.DEFAULT_TIMEOUT_INTERVAL = 200 beforeEach -> window.fixturesProject = new Project(require.resolve('fixtures')) window.resetTimeouts() - pathsWithSubscriptions = [] # make editor display updates synchronous spyOn(Editor.prototype, 'requestDisplayUpdate').andCallFake -> @updateDisplay() @@ -39,10 +37,8 @@ afterEach -> delete window.rootView if window.rootView $('#jasmine-content').empty() window.fixturesProject.destroy() - - waits(0) - runs -> -# ensureNoPathSubscriptions() + ensureNoPathSubscriptions() + waits(0) # yield to ui thread to make screen update more frequently window.keymap.bindKeys '*', 'meta-w': 'close' $(document).on 'close', -> window.close() @@ -51,20 +47,10 @@ $('html,body').css('overflow', 'auto') # Don't load user configuration in specs, because it's variable RootView.prototype.loadUserConfiguration = -> -for klass in [Directory, File] - klass.prototype.originalOn = klass.prototype.on - klass.prototype.on = (args...) -> - pathsWithSubscriptions.push(this) if @subscriptionCount() == 0 - @originalOn(args...) - ensureNoPathSubscriptions = -> - totalSubscriptionCount = 0 - for path in pathsWithSubscriptions - totalSubscriptionCount += path.subscriptionCount() - console.log "Non-zero subscription count on", path if path.subscriptionCount() > 0 - - if totalSubscriptionCount > 0 - throw new Error("Total path subscription count was #{totalSubscriptionCount}, when it should have been 0.\nSee console for details.") + watchedPaths = $native.getWatchedPaths() + if watchedPaths.length > 0 + throw new Error("Leaking subscriptions for paths: " + watchedPaths.join(", ")) # Use underscore's definition of equality for toEqual assertions jasmine.Env.prototype.equals_ = _.isEqual