-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Process safe deleteRealm and assetFile (#5417)
- Use SharedRealm::call_with_lock() to make sure deleteRealm and assetFile calls are process safe. - Also those calls will throw if the Realm instance on the sync client thread is still opened.
- Loading branch information
Showing
21 changed files
with
338 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
realm/realm-library/src/androidTest/java/io/realm/internal/OsObjectStoreTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2017 Realm Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.realm.internal; | ||
|
||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import io.realm.RealmConfiguration; | ||
import io.realm.rule.TestRealmConfigurationFactory; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertFalse; | ||
import static junit.framework.Assert.assertTrue; | ||
import static junit.framework.Assert.fail; | ||
|
||
// Tests for OsObjectStore | ||
@RunWith(AndroidJUnit4.class) | ||
public class OsObjectStoreTests { | ||
@Rule | ||
public final TestRealmConfigurationFactory configFactory = new TestRealmConfigurationFactory(); | ||
@Rule | ||
public final ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Test | ||
public void callWithLock() { | ||
RealmConfiguration config = configFactory.createConfiguration(); | ||
|
||
// Return false if there are opened SharedRealm instance | ||
SharedRealm sharedRealm = SharedRealm.getInstance(config); | ||
assertFalse(OsObjectStore.callWithLock(config, new Runnable() { | ||
@Override | ||
public void run() { | ||
fail(); | ||
} | ||
})); | ||
sharedRealm.close(); | ||
|
||
final AtomicBoolean callbackCalled = new AtomicBoolean(false); | ||
assertTrue(OsObjectStore.callWithLock(config, new Runnable() { | ||
@Override | ||
public void run() { | ||
callbackCalled.set(true); | ||
} | ||
})); | ||
assertTrue(callbackCalled.get()); | ||
} | ||
|
||
// Test if a java exception can be thrown from the callback. | ||
@Test | ||
public void callWithLock_throwInCallback() { | ||
RealmConfiguration config = configFactory.createConfiguration(); | ||
final RuntimeException exception = new RuntimeException(); | ||
|
||
try { | ||
OsObjectStore.callWithLock(config, new Runnable() { | ||
@Override | ||
public void run() { | ||
throw exception; | ||
} | ||
}); | ||
fail(); | ||
} catch (RuntimeException e) { | ||
assertEquals(exception, e); | ||
} | ||
|
||
// The lock should be released after exception thrown | ||
final AtomicBoolean callbackCalled = new AtomicBoolean(false); | ||
assertTrue(OsObjectStore.callWithLock(config, new Runnable() { | ||
@Override | ||
public void run() { | ||
callbackCalled.set(true); | ||
} | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.