allow running all tests at the same path by invalidating all cached realm paths between test runs

This commit is contained in:
Ari Lazier 2015-10-12 16:43:05 -07:00
parent 4cfa8eae7b
commit aa035717a5
4 changed files with 40 additions and 7 deletions

View File

@ -860,6 +860,11 @@
INFOPLIST_FILE = tests/Info.plist; INFOPLIST_FILE = tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0; IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-isystem",
core/include,
);
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
}; };
name = Debug; name = Debug;
@ -875,6 +880,11 @@
INFOPLIST_FILE = tests/Info.plist; INFOPLIST_FILE = tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0; IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-isystem",
core/include,
);
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
}; };
name = Release; name = Release;
@ -982,6 +992,11 @@
INFOPLIST_FILE = tests/Info.plist; INFOPLIST_FILE = tests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0; IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-isystem",
core/include,
);
PRODUCT_NAME = "$(TARGET_NAME)"; PRODUCT_NAME = "$(TARGET_NAME)";
}; };
name = GCov_Build; name = GCov_Build;

View File

@ -198,7 +198,9 @@ bool Realm::update_schema(std::unique_ptr<Schema> schema, uint64_t version)
auto migration_function = [&](Group*, Schema&) { auto migration_function = [&](Group*, Schema&) {
SharedRealm old_realm(new Realm(old_config)); SharedRealm old_realm(new Realm(old_config));
auto updated_realm = shared_from_this(); auto updated_realm = shared_from_this();
m_config.migration_function(old_realm, updated_realm); if (m_config.migration_function) {
m_config.migration_function(old_realm, updated_realm);
}
}; };
try { try {
@ -438,6 +440,19 @@ void RealmCache::cache_realm(SharedRealm &realm, std::thread::id thread_id)
} }
} }
void RealmCache::invalidate_all()
{
std::lock_guard<std::mutex> lock(m_mutex);
for (auto &path_realms:m_cache) {
for (auto &realm_iter:path_realms.second) {
if (auto realm = realm_iter.second.lock()) {
realm->invalidate();
}
}
}
}
void RealmCache::clear() void RealmCache::clear()
{ {
std::lock_guard<std::mutex> lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);

View File

@ -131,6 +131,7 @@ namespace realm {
SharedRealm get_any_realm(const std::string &path); SharedRealm get_any_realm(const std::string &path);
void remove(const std::string &path, std::thread::id thread_id); void remove(const std::string &path, std::thread::id thread_id);
void cache_realm(SharedRealm &realm, std::thread::id thread_id = std::this_thread::get_id()); void cache_realm(SharedRealm &realm, std::thread::id thread_id = std::this_thread::get_id());
void invalidate_all();
void clear(); void clear();
private: private:

View File

@ -20,6 +20,8 @@
#import "RJSUtil.hpp" #import "RJSUtil.hpp"
#import "RJSRealm.hpp" #import "RJSRealm.hpp"
#import "shared_realm.hpp"
NSString *RealmPathForFile(NSString *fileName) { NSString *RealmPathForFile(NSString *fileName) {
#if TARGET_OS_IPHONE #if TARGET_OS_IPHONE
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
@ -30,10 +32,9 @@ NSString *RealmPathForFile(NSString *fileName) {
return [path stringByAppendingPathComponent:fileName]; return [path stringByAppendingPathComponent:fileName];
} }
static NSString *s_testPrefix;
NSString *TestRealmPath() { NSString *TestRealmPath() {
return RealmPathForFile([s_testPrefix stringByAppendingPathComponent:@"test.realm"]); return RealmPathForFile(@"test.realm");
} }
static void DeleteOrThrow(NSString *path) { static void DeleteOrThrow(NSString *path) {
@ -72,10 +73,8 @@ static JSClassRef s_globalClass;
- (void)setUp { - (void)setUp {
[super setUp]; [super setUp];
s_testPrefix = [[NSUUID UUID] UUIDString]; NSString *defaultDir = [[NSString stringWithUTF8String:RJSDefaultPath().c_str()] stringByDeletingLastPathComponent];
NSString *defaultDir = RealmPathForFile(s_testPrefix);
[[NSFileManager defaultManager] createDirectoryAtPath:defaultDir withIntermediateDirectories:YES attributes:nil error:nil]; [[NSFileManager defaultManager] createDirectoryAtPath:defaultDir withIntermediateDirectories:YES attributes:nil error:nil];
RJSSetDefaultPath([defaultDir stringByAppendingPathComponent:@"default.realm"].UTF8String);
JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(NULL, s_globalClass); JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(NULL, s_globalClass);
self.context = [JSContext contextWithJSGlobalContextRef:ctx]; self.context = [JSContext contextWithJSGlobalContextRef:ctx];
@ -90,6 +89,9 @@ static JSClassRef s_globalClass;
- (void)tearDown { - (void)tearDown {
self.context = nil; self.context = nil;
realm::Realm::s_global_cache.invalidate_all();
realm::Realm::s_global_cache.clear();
DeleteRealmFilesAtPath(TestRealmPath()); DeleteRealmFilesAtPath(TestRealmPath());
DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str())); DeleteRealmFilesAtPath(@(RJSDefaultPath().c_str()));