Skip to content
This repository was archived by the owner on Jun 6, 2019. It is now read-only.

Update to Kotlin 1.3.11 and coroutines 1.0.1 #34

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
buildscript {
repositories {
mavenCentral()
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap' }
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.0-rc-57'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.0'
}
}

Expand All @@ -17,14 +16,12 @@ sourceCompatibility = JavaVersion.VERSION_1_6

repositories {
mavenCentral()
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap' }
maven { url 'https://kotlin.bintray.com/kotlinx' }
}

dependencies {
api 'com.squareup.retrofit2:retrofit:2.4.0'
api 'org.jetbrains.kotlin:kotlin-stdlib:1.3.0-rc-57'
api 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.26.1-eap13'
api 'org.jetbrains.kotlin:kotlin-stdlib:1.3.0'
api 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'

testImplementation 'junit:junit:4.12'
testImplementation 'com.squareup.okhttp3:mockwebserver:3.11.0'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GROUP=com.jakewharton.retrofit
VERSION_NAME=0.9.3-SNAPSHOT
VERSION_NAME=1.0.0-SNAPSHOT

POM_NAME=Retrofit 2 Kotlin Coroutine Adapter
POM_DESCRIPTION=A CallAdapter.Factory for Kotlin coroutine's Deferred.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.jakewharton.retrofit2.adapter.kotlin.coroutines

import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Deferred
import retrofit2.Call
Expand Down Expand Up @@ -87,7 +88,12 @@ class CoroutineCallAdapterFactory private constructor() : CallAdapter.Factory()
val deferred = CompletableDeferred<T>()

deferred.invokeOnCompletion {
if (deferred.isCancelled) {
/**
* The [call] should not be marked as cancelled when it completes exceptionally.
* If the cause of cancellation is [CancellationException], then the [CompletableDeferred] is
* considered to be _cancelled normally_.
*/
if (deferred.isCancelled && it is CancellationException) {
call.cancel()
}
}
Expand Down Expand Up @@ -120,7 +126,12 @@ class CoroutineCallAdapterFactory private constructor() : CallAdapter.Factory()
val deferred = CompletableDeferred<Response<T>>()

deferred.invokeOnCompletion {
if (deferred.isCancelled) {
/**
* The [call] should not be marked as cancelled when it completes exceptionally.
* If the cause of cancellation is [CancellationException], then the [CompletableDeferred] is
* considered to be _cancelled normally_.
*/
if (deferred.isCancelled && it is CancellationException) {
call.cancel()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CancelTest {
val deferred = adapter.adapt(call)
call.completeWithException(IOException())
assertFalse(call.isCanceled)
Copy link
Author

Choose a reason for hiding this comment

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

The call is cancelled in coroutines 0.27.0+ because CompletedExceptionally is considered a "cancelled" state

assertTrue(deferred.isCompletedExceptionally)
assertTrue(deferred.isCancelled && deferred.isCompleted)
Copy link
Author

Choose a reason for hiding this comment

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

See Deferred in coroutines 0.27.0 for more details

}

@Test fun cancelOnCancel() {
Expand Down