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

[Bug]: The unit test offlineFirstTopicsRepository_toggle_followed_topics_logic_delegates_to_nia_preferences() failed #98

Closed
3 tasks done
Bwaim opened this issue Jun 2, 2022 · 10 comments
Labels
bug Something isn't working waiting Waiting on (blocked by) someone or something

Comments

@Bwaim
Copy link

Bwaim commented Jun 2, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Is there a StackOverflow question about this issue?

  • I have searched StackOverflow

What happened?

Windows environment
Running the unit test offlineFirstTopicsRepository_toggle_followed_topics_logic_delegates_to_nia_preferences() fails.
There is an exception thrown that is cached but the log of this exception causes crash because it's not mocked, the original exception is :

java.io.IOException: Unable to rename C:\xxxxx\Temp\junit2206256684596171052\user_preferences_test.pb.tmp.This likely means that there are multiple instances of DataStore for this file. Ensure that you are only creating a single instance of datastore for this file.

The stacktrace :

androidx.datastore.core.SingleProcessDataStore.writeData$datastore_core(SingleProcessDataStore.kt:433)
androidx.datastore.core.SingleProcessDataStore.transformAndWrite(SingleProcessDataStore.kt:410)
androidx.datastore.core.SingleProcessDataStore.access$transformAndWrite(SingleProcessDataStore.kt:76)
androidx.datastore.core.SingleProcessDataStore$transformAndWrite$1.invokeSuspend(SingleProcessDataStore.kt)
\b\b\b(Coroutine boundary.\b(\b)
com.google.samples.apps.nowinandroid.core.datastore.NiaPreferences.toggleFollowedTopicId(NiaPreferences.kt:46)
com.google.samples.apps.nowinandroid.core.data.repository.OfflineFirstTopicsRepositoryTest$offlineFirstTopicsRepository_toggle_followed_topics_logic_delegates_to_nia_preferences$1.invokeSuspend(OfflineFirstTopicsRepositoryTest.kt:190)

I think the problem is because the test was executed under a Windows environment and the rename failed on that platform (SingleProcessDataStore.kt : 432) :

            if (!scratchFile.renameTo(file)) {
                throw IOException(
                    "Unable to rename $scratchFile." +
                        "This likely means that there are multiple instances of DataStore " +
                        "for this file. Ensure that you are only creating a single instance of " +
                        "datastore for this file."
                )
            }

Relevant logcat output

Method e in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details.
java.lang.RuntimeException: Method e in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details.
	at android.util.Log.e(Log.java)
	at com.google.samples.apps.nowinandroid.core.datastore.NiaPreferences.toggleFollowedTopicId(NiaPreferences.kt:59)
	at com.google.samples.apps.nowinandroid.core.datastore.NiaPreferences$toggleFollowedTopicId$1.invokeSuspend(NiaPreferences.kt)

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Bwaim Bwaim added the bug Something isn't working label Jun 2, 2022
@Rocksnake
Copy link
Contributor

I think the reason should be that two DataStore's writing at the same time, one of the renames seems to be failing, because the file no longer exists.

I think the place where the problem occurs should be in the TestDataStoreModule.kt file, We should instead create the DataStore once, then inject the instance where necessary or manage it as a singleton.
image

@alexvanyo
Copy link
Contributor

Hi, is this still an issue with the latest version?

@alexvanyo alexvanyo added the waiting Waiting on (blocked by) someone or something label Sep 2, 2022
@SimonMarquis
Copy link
Contributor

SimonMarquis commented Nov 18, 2022

FWIW, on a Windows machine, I'm unable to run the whole test suite. Many unit tests fail while SingleProcessDataStore tries to write the new data as shown above.

@SimonMarquis
Copy link
Contributor

@SimonMarquis
Copy link
Contributor

androidx.datastore:datastore-*:1.1.0-beta02 landed:

Fixed the issue where file rename fails during updateData in non-Android JVM environments. (b/203087070)
https://developer.android.com/jetpack/androidx/releases/datastore#1.1.0-beta02

Unfortunately, the issue persists...

@JackEblan
Copy link

Hello @SimonMarquis I am also experiencing this java.io.IOException: Unable to rename C:\Users\User\AppData\Local\Temp\junit481202955115791832\user_preferences_test.pb.tmp.This likely means that there are multiple instances of DataStore for this file. Ensure that you are only creating a single instance of datastore for this file. Is there a way to make this work on my Windows 10?

@SimonMarquis
Copy link
Contributor

No, but you can ⭐ & +1 this issue: https://issuetracker.google.com/issues/203087070

@SimonMarquis
Copy link
Contributor

@Bwaim a fix has been implemented in #1542, can you check if the fix works for you as well?

@Bwaim
Copy link
Author

Bwaim commented Jul 11, 2024

@SimonMarquis unfortunately I can't check as I don't have a Windows anymore 😞

@SimonMarquis
Copy link
Contributor

@Bwaim do you still reproduce this issue?
It should have been fixed with:

If that is the case, you can close the issue.
Thanks 🙏

@Bwaim Bwaim closed this as completed Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working waiting Waiting on (blocked by) someone or something
Projects
None yet
Development

No branches or pull requests

5 participants