All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.apollographql.apollo.mockserver.QueueMockServerHandler.kt Maven / Gradle / Ivy

package com.apollographql.apollo.mockserver

import kotlinx.atomicfu.locks.reentrantLock
import kotlinx.atomicfu.locks.withLock


internal class QueueMockServerHandler : MockServerHandler {

  private val lock = reentrantLock()
  private val queue = ArrayDeque()

  fun enqueue(response: MockResponse) {
    lock.withLock {
      queue.add(response)
    }
  }

  override fun handle(request: MockRequestBase): MockResponse {
    var response = lock.withLock {
      queue.removeFirstOrNull() ?: error("No more responses in queue")
    }

    if (request is WebsocketMockRequest) {
      response = response.replaceWebSocketHeaders(request)
    }

    return response
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy