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

Coroutines and necessary Ktlint configuration #39

Merged
merged 4 commits into from
Jan 25, 2024

Conversation

hamorillo
Copy link
Contributor

@hamorillo hamorillo commented Jan 24, 2024

#30

Using Kotlin Coroutines will allow us to improve our testing possibilities. This PR adds several tests to the current GravatarApi.

I've created a wrapper for the Android logger and injected it in the GravatarApi using the container. That way, we don't need an Android class in the unit test, and why not? We can verify that the Logger is invoked (if needed).

This PR also modified our Ktlint rules to remove some unnecessary "new line" requirements.

import org.junit.runner.Description
import org.junit.runners.model.Statement

class GravatarSdkTest : TestRule {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This TestRule sets the basic container to be able to test the GravatarApi.

Comment on lines +8 to +9
ktlint_function_signature_body_expression_wrapping = default
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it looks better in the same line when possible, but let me know what you think.

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

Comment on lines +10 to +11
ktlint_standard_multiline-expression-wrapping = disabled
ktlint_standard_string-template-indent = disabled
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allow to keep the expression in the same line when it fits. Example:

@Test
fun `given a file, email and accessToken when uploading avatar then Gravatar service is invoked`() = runTest {

Without disabling those rules:

@Test
fun `given a file, email and accessToken when uploading avatar then Gravatar service is invoked`() = 
    runTest {

@@ -27,6 +26,8 @@ class GravatarApi(private val okHttpClient: OkHttpClient? = null) {
UNKNOWN,
}

val coroutineScope = CoroutineScope(GravatarSdkContainer.instance.dispatcherDefault)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Injecting the dispatchers is a good practice to be able to replace them when unit testing.

@hamorillo hamorillo changed the title DRAFT - Coroutines and necessary Ktlint configuration Coroutines and necessary Ktlint configuration Jan 24, 2024
@hamorillo hamorillo requested a review from maxme January 24, 2024 14:15
if (response.isSuccessful) {
gravatarUploadListener.onSuccess()
} else {
// Log the response body for debugging purposes if the response is not successful
Log.w(
GravatarSdkContainer.instance.logger.w(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to alias GravatarSdkContainer.instance.logger to something shorter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've refactored this part. Rethinking about this, we probably don't need to inject the logger; the main reason to have a wrapper around it would be to have better control over our library logs and send some error analytics when/if needed. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably don't need to inject the logger; the main reason to have a wrapper around it would be to have better control over our library logs and send some error analytics when/if needed

It sounds like this falls into the premature optimization category. Let's keep it simple for now, we can inject a logger abstraction later if we need to.

Copy link
Contributor

@maxme maxme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:
we can revisit logging stuff later

Comment on lines +8 to +9
ktlint_function_signature_body_expression_wrapping = default
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

@hamorillo hamorillo merged commit f81e731 into trunk Jan 25, 2024
1 check failed
@hamorillo hamorillo deleted the hamorillo/13-corutines branch January 25, 2024 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants