Skip to content

Add cold flow examples#230

Open
ashtanko wants to merge 2 commits into
mainfrom
feature/add_coroutine_exmaples
Open

Add cold flow examples#230
ashtanko wants to merge 2 commits into
mainfrom
feature/add_coroutine_exmaples

Conversation

@ashtanko

@ashtanko ashtanko commented Jan 17, 2026

Copy link
Copy Markdown
Owner

Summary by CodeRabbit

  • New Features

    • Debounced search suggestion flow with simulated latency
    • Continuous stock price feed with configurable refresh interval
  • Documentation

    • Updated Kotlin version badge and refreshed project metrics/badges
  • Tests

    • Added unit tests covering search suggestions and stock price streams
  • Chores

    • CI workflow adjusted (coverage reporting step disabled)
    • Local development config now excluded from version control
  • Style

    • Updated editor/import formatting rules

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 17, 2026

Copy link
Copy Markdown

Walkthrough

Adds cold coroutine flows for search suggestions and stock prices with implementations and tests; updates editor import settings, CI step (Codacy reporter commented), README badges, and .gitignore/local.properties handling.

Changes

Cohort / File(s) Summary
Editor & Kotlin Import Settings
\.editorconfig
Replaced Kotlin wildcard import layout with empty value and set star-import thresholds: ij_kotlin_name_count_to_use_star_import = 999, ij_kotlin_name_count_to_use_star_import_for_members = 999.
CI Workflow
.github/workflows/build.yml
Commented out the Codacy coverage reporter step; SonarCloud step preserved.
Gitignore / Local Config
.gitignore, local.properties
Added local.properties to .gitignore. Removed the repository local.properties file that contained SDK path.
Documentation / Badges
README.md, config/main.md
Bumped Kotlin badge from 2.2.202.2.21, added coverage/duplication badges, and updated displayed metrics.
API Surface Summary
api/Kotlin-Lab.api
Declares new public types introduced by the Kotlin sources: SearchApi, FakeSearchApi, StockApi, tickerFlow, and StockRepository.
Search Flow Implementation
src/main/kotlin/.../flow/cold/SearchApi.kt, src/main/kotlin/.../flow/cold/SearchManager.kt
Added SearchApi interface and FakeSearchApi (500ms delay, in-memory filter). Added SearchManager that exposes getSuggestions(queryFlow: Flow<String>, debounceMillis: Long = 300L): Flow<List<String>> — applies debounce, blank filtering, distinctUntilChanged, flatMapLatest to call api.search, and catches errors to emit empty lists.
Stock Flow Implementation
src/main/kotlin/.../flow/cold/StockApi.kt
Added StockApi interface (suspend fetchPrice(symbol): Double), top-level tickerFlow(interval: Long): Flow<Unit> emitting at intervals, and StockRepository(api, refreshInterval) with getStockPrice(symbol): Flow<Double> mapping ticker emissions to price fetches.
Unit Tests
src/test/kotlin/.../flow/cold/SearchManagerTest.kt, src/test/kotlin/.../flow/cold/StockRepositoryTest.kt
Added SearchManagerTest (debounce and duplicate-suppression tests) using MutableSharedFlow, Turbine, and virtual time. Added StockRepositoryTest (endless-flow cancellation and finite take-based test) using MockK, Turbine, and coroutine test utilities.

Sequence Diagram(s)

sequenceDiagram
    participant Input as Query Flow
    participant SM as SearchManager
    participant API as SearchApi
    participant Output as Suggestions Flow

    Input->>SM: emit query
    SM->>SM: debounce (300ms)
    SM->>SM: filter blank
    SM->>SM: distinctUntilChanged
    SM->>API: search(query)
    API-->>SM: List<String>
    SM->>Output: emit suggestions
    Note over SM: on error → emit empty list
Loading
sequenceDiagram
    participant Ticker as tickerFlow
    participant SR as StockRepository
    participant API as StockApi
    participant Output as Price Flow

    loop every refreshInterval
        Ticker->>SR: emit Unit
        SR->>API: fetchPrice(symbol)
        API-->>SR: Double (price)
        SR->>Output: emit price
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 I nibble bits of code and cheer,
Debounced queries whisper near,
Tick-tock prices hop and flow,
Tests with Turbine help me know,
Hooray — cold coroutines on show! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add cold flow examples' accurately summarizes the main change: introducing new cold flow examples with SearchApi, SearchManager, and StockApi implementations.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/add_coroutine_exmaples

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In
`@src/main/kotlin/dev/shtanko/concurrency/coroutines/flow/cold/SearchManager.kt`:
- Around line 22-25: The current placement of catch after flatMapLatest causes
per-query errors to potentially terminate the outer flow; move the error
handling inside the flatMapLatest lambda so each API call is wrapped with its
own try-recover: replace the inner mapping that uses flow {
emit(api.search(query)) } so that the flow produced per query handles exceptions
(catch or try/catch) and emits emptyList() on failure; modify the block around
flatMapLatest/flow { emit(api.search(query)) } to catch exceptions from
api.search(query) locally rather than relying on the outer .catch.
🧹 Nitpick comments (2)
src/main/kotlin/dev/shtanko/concurrency/coroutines/flow/cold/StockApi.kt (2)

12-17: Consider validating the interval parameter.

If interval is zero or negative, this could lead to unexpected behavior—either a tight loop with no delay or undefined delay() behavior. Adding a require check would make the function more robust.

♻️ Suggested validation
-fun tickerFlow(interval: Long) = flow {
+fun tickerFlow(interval: Long): Flow<Unit> {
+    require(interval > 0) { "Interval must be positive, was $interval" }
+    return flow {
         while (true) {
             emit(Unit)
             delay(interval)
         }
     }
+}

19-25: Consider adding error handling for resilience.

If api.fetchPrice throws an exception, the flow will terminate. For a continuous price stream, you might want to catch and handle errors to keep the flow alive. Since this is an example file, this may be intentional for simplicity.

♻️ Optional: Add retry/catch for resilience
+import kotlinx.coroutines.flow.catch
+import kotlinx.coroutines.flow.retry
+
 class StockRepository(
     private val api: StockApi,
     private val refreshInterval: Long = 5000L,
 ) {
     fun getStockPrice(symbol: String): Flow<Double> =
-        tickerFlow(refreshInterval).map { api.fetchPrice(symbol) }
+        tickerFlow(refreshInterval)
+            .map { api.fetchPrice(symbol) }
+            .retry(3) { e -> e is IOException }

Comment thread src/main/kotlin/dev/shtanko/concurrency/coroutines/flow/cold/SearchManager.kt Outdated
…earchManager.kt

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
37.9% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

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.

1 participant