realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

realm.close() Does not work when the automation test cases run in application which uses realm.close

Open PrabhatPandey opened this issue 11 years ago • 13 comments

Here is the set up method of the android test project

    @Override
    public void setUp() throws Exception {
        Log.d("Status:", "Setting up the env.");

        RealmConfiguration aRealmConfiguration = new RealmConfiguration.Builder(
                getInstrumentation().getTargetContext()).build();
        Realm.setDefaultConfiguration(aRealmConfiguration);
        Realm.getInstance(aRealmConfiguration).close();
        Realm aRealm = Realm.getInstance(aRealmConfiguration);

        aRealm.close();


        try {

            if (Realm.getInstance(aRealmConfiguration) != null) {
                Realm.getInstance(aRealmConfiguration).close();
                Realm.deleteRealm(aRealmConfiguration);
            }
        } catch (Exception ex) {
            throw ex;
            // No Realm file to remove.
        }
        Log.d("Status:", "Cleared the app data");
        solo = new Solo(getInstrumentation(), getActivity());
        Log.d("Status:", "Passed activity to solo.");
    }

When I am running the each test individually , It works perfectly. It cleans the application data. but when I run all of them it starts giving this error java.lang.IllegalStateException: It's not allowed to delete the file associated with an open Realm. Remember to close() all the instances of the Realm before deleting its file.

at io.realm.BaseRealm.deleteRealm(BaseRealm.java:519)
at io.realm.Realm.deleteRealm(Realm.java:1088)
at com.iryd.TestIRYDUserApplication.setUp(TestIRYDUserApplication.java:47)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

Am I doing something wrong ?

PrabhatPandey avatar Oct 13 '15 12:10 PrabhatPandey

Do you open your Realm in an Application subclass? That class will be created when running an instrumentation test, which can give you the error you are seeing.

cmelchior avatar Oct 13 '15 18:10 cmelchior

if (Realm.getInstance(aRealmConfiguration) == null) {
    Realm.getInstance(aRealmConfiguration).close();
    Realm.deleteRealm(aRealmConfiguration);
}

I think this condition never be true since Realm.getInstance(...) always returns non-null value.

zaki50 avatar Oct 13 '15 23:10 zaki50

@cmelchior I am not opening my realm in application subclass. I am performing blackbox testing here. I am just trying to delete the data after each test run. If important I can provide you the source code.

Realm.deleteRealm(aRealmConfiguration) does not close the connections, which I need in my case.

PrabhatPandey avatar Oct 14 '15 05:10 PrabhatPandey

@zaki50 it was mistake from my side, I have changed the condition now. It still gives me the same error message.

PrabhatPandey avatar Oct 14 '15 06:10 PrabhatPandey

@PrabhatPandey If you can send the source code that will help a lot. You can send it to [email protected].

cmelchior avatar Oct 14 '15 06:10 cmelchior

@cmelchior "Do you open your Realm in an Application subclass? That class will be created when running an instrumentation test, which can give you the error you are seeing."

Developer was not closing the database when user closes the application. He added OnTerminate() method in application and closed the connection.

This following code worked for cleaning the application data.

@Override public void setUp() throws Exception { Log.d("Status:", "Setting up the env.");

    RealmConfiguration aRealmConfiguration = new RealmConfiguration.Builder(
            getInstrumentation().getTargetContext()).build();
    Realm.setDefaultConfiguration(aRealmConfiguration);
    try {
        Realm.deleteRealm(aRealmConfiguration);
    } catch (Exception ex) {
        throw ex;
    }

    solo = new Solo(getInstrumentation(), getActivity());
    Log.d("Status:", "Passed activity to solo.");
}

Thanks for the help guys. My issue is resolved now.

PrabhatPandey avatar Oct 14 '15 09:10 PrabhatPandey

@PrabhatPandey be careful OnTerminate is not called on devices

nhachicha avatar Oct 16 '15 09:10 nhachicha

@nhachicha Developer added Realm.getDefaultInstance().close and it resolved the issue. Are you saying this might get repeated again ?

PrabhatPandey avatar Oct 16 '15 18:10 PrabhatPandey

This issue is getting repeated again now. This happens when I log-in in the application using my credentials. Right after I log-in It starts throwing this Error again.

@cmelchior I have sent you the source code of my android test project along with the application apk file.

PrabhatPandey avatar Oct 23 '15 06:10 PrabhatPandey

@PrabhatPandey Quickly browsing through your test app, my advice it to open the Realm in setUp() (realm = Realm.getDefaultInstance())) and close it in tearDown() (realm.close()`).

kneth avatar Oct 23 '15 09:10 kneth

Hey guys, This is how I am initialising the realm instance. Realm.setDefaultConfiguration(new RealmConfiguration.Builder(context).build());

and this is how I am closing it onTerminate() Realm.getDefaultInstance().close();

What @kneth said, is not working for @PrabhatPandey

dekaustubh avatar Oct 23 '15 11:10 dekaustubh

Realm.getDefaultInstance().close() will open a new instance which is closed. But the initial Realm instance is not closed. My previous comment was trying to say that every call to getDefaultInstance() must be balanced with a call to close().

kneth avatar Oct 23 '15 11:10 kneth

Thanks @kneth Realm was not being closed for every instance that is why this error was getting repeated. I am closing this issue one more time.

PrabhatPandey avatar Oct 23 '15 19:10 PrabhatPandey