-
Notifications
You must be signed in to change notification settings - Fork 31
STITCH-2510 Java SDK: Alter AuthListener to better represent User lifecycle #97
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solid start, mainly had some naming feedback to bring the interface to parity with JavaScript, and logic changes in CoreStitchAuth
. I think some of this may have been written before we decided to have onActiveUserChanged
be called everywhere the active user changes, including login and logout.
android/core/src/main/java/com/mongodb/stitch/android/core/auth/StitchAuthListener.java
Outdated
Show resolved
Hide resolved
android/core/src/main/java/com/mongodb/stitch/android/core/auth/internal/StitchAuthImpl.java
Show resolved
Hide resolved
android/core/src/main/java/com/mongodb/stitch/android/core/auth/StitchAuthListener.java
Outdated
Show resolved
Hide resolved
android/core/src/main/java/com/mongodb/stitch/android/core/auth/StitchAuthListener.java
Show resolved
Hide resolved
core/sdk/src/main/java/com/mongodb/stitch/core/auth/internal/CoreStitchAuth.java
Show resolved
Hide resolved
core/sdk/src/main/java/com/mongodb/stitch/core/auth/internal/CoreStitchAuth.java
Show resolved
Hide resolved
); | ||
// reinitialize the DataSynchronizer entirely. | ||
// any auth event will trigger this. | ||
this.dataSynchronizer.reinitialize( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[minor] once you fix CoreStitchAuth
to trigger onActiveUserChanged
event in all the places where a logout and login can happen, you should only do this reinitialization on the ACTIVE_USER_CHANGED
case, and have default
be a no-op. Otherwise, the reinitialize
will get triggered multiple times on a single login or logout.
); | ||
// reinitialize the DataSynchronizer entirely. | ||
// any auth event will trigger this. | ||
this.dataSynchronizer.reinitialize( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[q] if there is no active user after the active user has changed, shouldn't we call a different method that just stops the data synchronizer rather than reinitializes it? I see right now that the data synchronizer just ends up being bound to an "unbound" data directory. I guess that's fine but wouldn't it be cleaner if the data synchronizer just didn't run at all when logged out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It won't run anyway. This won't be an issue until we get to sync cadence. In the interim, this is fine.
server/core/src/main/java/com/mongodb/stitch/server/core/auth/internal/StitchAuthImpl.java
Outdated
Show resolved
Hide resolved
import java.util.concurrent.TimeUnit | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class StitchAppClientIntTests : BaseStitchAndroidIntTest() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[task] I think you should probably test more cases auth listener cases.
check out the JS PR, I added a new file of StitchAuthListenerIntTests
, and for each method of the listener, I have a test method that simulates the different ways that event can be triggered, and ensures that the event method is called a certain number of times.
it sounds like a lot but it should be pretty simple to write, and it's a lot cleaner than trying to mix in with existing integration tests
@Override | ||
public void onUserLoggedIn(final StitchAuth auth, | ||
final StitchUser loggedInUser) { | ||
onRebindEvent(new AuthEvent.UserLoggedIn<>(loggedInUser)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should you be rebinding on all of these?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is up to the service to determine what to do with the information given.
@adamchel PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple nits, and you should also add tests to the server SDK, but LGTM otherwise!
.../coretest/src/androidTest/java/com/mongodb/stitch/android/core/StitchAuthListenerIntTests.kt
Show resolved
Hide resolved
.../coretest/src/androidTest/java/com/mongodb/stitch/android/core/StitchAuthListenerIntTests.kt
Show resolved
Hide resolved
.../coretest/src/androidTest/java/com/mongodb/stitch/android/core/StitchAuthListenerIntTests.kt
Outdated
Show resolved
Hide resolved
...java/com/mongodb/stitch/core/services/mongodb/remote/internal/CoreRemoteMongoClientImpl.java
Outdated
Show resolved
Hide resolved
server/core/src/main/java/com/mongodb/stitch/server/core/auth/internal/StitchAuthImpl.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! Much needed cleanup that will make this code a lot more stable.
Had a few minor questions/concerns but nothing that should block the merge. Would appreciate some answers or the chance to discuss before we merge. Might just require some future work.
doneOnce = true; | ||
line = ""; | ||
} catch (IOException e) { | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[q] why not throw an exception? if we're keeping this, update the comment for this method to explain when this may be null, and add a @Nullable
annotation
while (runnerThread.isAlive()) { | ||
runnerThread.interrupt(); | ||
try { | ||
System.out.println("killing runner"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[q] was this for debugging? delete if so. if this would be useful for users, make this an actual log item
} | ||
|
||
final String userId = appInfo.getAuthMonitor().isLoggedIn() | ||
final String userId = appInfo.getAuthMonitor().tryIsLoggedIn() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[q] does this need to be a tryIsLoggedIn
? I'm concerned that this would cause a real user to end up having their data be in an "unbound"
data directory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the current state of our codebase, it would not be possible for this to happen. However, maybe I'll make a note here for "in the future".
ctx.isLoggedIn = true | ||
assertFalse(namespaceChangeStreamListener.openStream()) | ||
verify(nsConfigMock, times(1)).synchronizedDocumentIds | ||
verify(nsConfigMock, times(3)).synchronizedDocumentIds |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[q] what changed to make this happen? this isn't a performance hit right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... it is a slight performance hit. I moved getSynchronizedDocumentIds
to the top of openStream
to pre-read them before taking the lock. But that may not be necessary anymore.
@Override | ||
public boolean isLoggedIn() { | ||
return getAuth().isLoggedIn(); | ||
public boolean isLoggedIn() throws InterruptedException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[q] If a user ever calls isLoggedIn()
method on a StitchAppClient
, is there a case where that can throw? should we mark the method as throws
in the interface and explain when it could happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, all of this is internal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah you're right, there's no isLoggedIn on the app client itself
} | ||
} catch (InterruptedException e) { | ||
System.err.println("Stream authentication was interrupted."); | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[q] are we sure we want to return this? why not just throw the exception? This seems like a null pointer exception waiting to happen somewhere upstream. What happens if the user calls watch
and the stream got interrupted? I think it would be better for an error to happen rather than getting a null result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment, that would have to happen on the users accord. We should probably throw an exception.
throw new StitchClientException(StitchClientErrorCode.MUST_AUTHENTICATE_FIRST); | ||
} | ||
} catch (InterruptedException e) { | ||
System.err.println("Stream authentication was interrupted."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make this a log statement or get rid of the print
No description provided.