Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dc5290f

Browse files
Add small change to Activity4SupervisorJob.kt and changes in tests
1 parent 00433a5 commit dc5290f

File tree

7 files changed

+52
-21
lines changed

7 files changed

+52
-21
lines changed

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/chapter4_supervisorjob/Activity4SupervisorJob.kt‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class Activity4SupervisorJob : AppCompatActivity() {
2424
*
2525
* [SupervisorJob] does not mean child coroutines will have a [SupervisorJob]
2626
* with a scope builder method like [CoroutineScope.launch]
27+
*
28+
* ### Try removing [CoroutineScope.launch] function that [childJob2] invokes to see that supervisor only affect the context it's attached to
29+
*
2730
*/
2831
private var parentJob: Job = SupervisorJob()
2932

@@ -116,7 +119,7 @@ class Activity4SupervisorJob : AppCompatActivity() {
116119

117120
}
118121

119-
// Invoked when a job completes from 🔥 Thread exception caught
122+
// Invoked when a job completes with or without 🔥 Thread exception caught
120123
currentJob?.invokeOnCompletion {
121124

122125
println("👍 invokeOnCompletion() Exception $it, in thread: ${Thread.currentThread().name}")
@@ -166,12 +169,32 @@ class Activity4SupervisorJob : AppCompatActivity() {
166169
val childSupervisorJob = SupervisorJob()
167170
childJob2 = myCoroutineScope.launch(childSupervisorJob ) {
168171

172+
println("⛱ childJob2 context: $this")
173+
169174
launch {
170175
displayNumbers(binding.tvChildJobResult2)
171176
}
172177

173-
delay(2000)
174-
throw RuntimeException("Child 2 threw RuntimeException")
178+
179+
/*
180+
🔥🔥 Despite the fact which inner coroutines exception occurs, as long as parent job
181+
is launched with SupervisorJob, only up to that coroutines are canceled
182+
183+
childJob1 still runs after exception
184+
185+
*/
186+
launch {
187+
188+
println("🤨 childJob2 inner context: $this")
189+
190+
// Exception can happen here
191+
delay(2000)
192+
throw RuntimeException("Child 2 threw RuntimeException")
193+
}
194+
195+
// Exception can also happen here
196+
// delay(2000)
197+
// throw RuntimeException("Child 2 threw RuntimeException")
175198

176199
}
177200

‎Tutorial1-1CoroutinesBasics/src/main/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/util/BindingAdapters.kt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.smarttoolfactory.tutorial1_1coroutinesbasics.util
22

33
import android.view.View
4-
import android.widget.Button
54
import android.widget.TextView
65
import androidx.databinding.BindingAdapter
76
import com.smarttoolfactory.tutorial1_1coroutinesbasics.model.ViewState
@@ -24,7 +23,7 @@ fun TextView.postState(viewState: ViewState<String>?) {
2423
viewState?.let {
2524
text = if (viewState.shouldShowErrorMessage()) {
2625
viewState.getErrorMessage()
27-
}else {
26+
}else {
2827
viewState.data
2928
}
3029
}

‎Tutorial1-1CoroutinesBasics/src/test/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/chapter5_viewmodel/CoroutinesViewModelWithCustomScopeTest.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.smarttoolfactory.tutorial1_1coroutinesbasics.chapter5_viewmodel
22

33
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
44
import com.google.common.truth.Truth
5-
import com.smarttoolfactory.tutorial1_1coroutinesbasics.util.TestCoroutineRule
5+
import com.smarttoolfactory.tutorial1_1coroutinesbasics.util.rules.TestCoroutineRule
66
import com.smarttoolfactory.tutorial1_1coroutinesbasics.util.getOrAwaitValue
77
import kotlinx.coroutines.Dispatchers
88
import org.junit.Before

‎Tutorial1-1CoroutinesBasics/src/test/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/chapter6_network/PostApiCoroutinesTest.kt‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import okhttp3.mockwebserver.MockResponse
1212
import org.junit.jupiter.api.AfterEach
1313
import org.junit.jupiter.api.BeforeEach
1414
import org.junit.jupiter.api.Test
15-
import org.junit.jupiter.api.assertThrows
1615
import retrofit2.Retrofit
1716
import retrofit2.converter.gson.GsonConverterFactory
1817

@@ -127,17 +126,26 @@ class PostApiCoroutinesTest : AbstractPostApiTest() {
127126
// }
128127

129128
// ❌ Also Fails
130-
val exception = testCoroutineScope.async {
131-
assertThrows<RuntimeException> {
132-
launch {
133-
postApi.getPosts()
134-
}
135-
}
136-
}.await()
129+
// val exception = testCoroutineScope.async {
130+
// assertThrows<RuntimeException> {
131+
// launch {
132+
// postApi.getPosts()
133+
// }
134+
// }
135+
// }.await()
136+
137+
val exception = try {
138+
testCoroutineScope.async {
139+
postApi.getPosts()
140+
}.await()
141+
null
142+
} catch (e: Exception) {
143+
e
144+
}
137145

138146

139147
// THEN
140-
Truth.assertThat(exception.message)
148+
Truth.assertThat(exception?.message)
141149
.isEqualTo("com.jakewharton.retrofit2.adapter.rxjava2.HttpException: HTTP 500 Server Error")
142150
}
143151

‎Tutorial1-1CoroutinesBasics/src/test/java/com/smarttoolfactory/tutorial1_1coroutinesbasics/chapter6_network/PostApiRxJavaTest.kt‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import com.smarttoolfactory.tutorial1_1coroutinesbasics.chapter6_network.api.Pos
77
import io.reactivex.observers.TestObserver
88
import io.reactivex.schedulers.Schedulers
99
import okhttp3.OkHttpClient
10+
import okhttp3.mockwebserver.Dispatcher
1011
import okhttp3.mockwebserver.MockResponse
12+
import okhttp3.mockwebserver.RecordedRequest
1113
import org.junit.jupiter.api.AfterEach
1214
import org.junit.jupiter.api.BeforeEach
1315
import org.junit.jupiter.api.Test
@@ -45,9 +47,6 @@ class PostApiRxJavaTest : AbstractPostApiTest() {
4547
@Test
4648
fun `Given we have a valid request, should be done to correct url`() {
4749

48-
@Test
49-
fun `Request has correct url`() {
50-
5150
// GIVEN
5251
enqueueResponse(200)
5352

@@ -59,7 +58,7 @@ class PostApiRxJavaTest : AbstractPostApiTest() {
5958
// THEN
6059
Truth.assertThat(request.path).isEqualTo("/posts")
6160

62-
}
61+
6362

6463
}
6564

@@ -141,4 +140,6 @@ class PostApiRxJavaTest : AbstractPostApiTest() {
141140
.isEqualTo("com.jakewharton.retrofit2.adapter.rxjava2.HttpException: HTTP 500 Server Error")
142141
}
143142

143+
144+
144145
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.smarttoolfactory.tutorial1_1coroutinesbasics.util
1+
package com.smarttoolfactory.tutorial1_1coroutinesbasics.util.rules
22

33
import okhttp3.mockwebserver.MockWebServer
44
import org.junit.rules.TestRule
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.smarttoolfactory.tutorial1_1coroutinesbasics.util
1+
package com.smarttoolfactory.tutorial1_1coroutinesbasics.util.rules
22

33
import kotlinx.coroutines.Dispatchers
44
import kotlinx.coroutines.ExperimentalCoroutinesApi

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /