Skip to content

Instantly share code, notes, and snippets.

@darkfrog26
Created June 19, 2015 20:13
Show Gist options
  • Save darkfrog26/32aeb05d92235496a53a to your computer and use it in GitHub Desktop.
Save darkfrog26/32aeb05d92235496a53a to your computer and use it in GitHub Desktop.
package org.scalarelational.h2
import java.util.concurrent.atomic.AtomicInteger
import org.powerscala.concurrent.Time
import org.scalarelational.column.property.{NotNull, Unique, AutoIncrement, PrimaryKey}
import org.scalarelational.model.Table
import org.scalatest.{Matchers, WordSpec}
/**
* @author Matt Hicks <matt@outr.com>
*/
class AsyncSpec extends WordSpec with Matchers {
import AsyncDatastore._
import scala.concurrent.ExecutionContext.Implicits.global
val running = new AtomicInteger(0)
"Async" should {
"insert a bunch of values asynchronously" in {
session {
(0 until 100).foreach {
case index => {
insert(users.name(s"User $index"), users.age(index)).async.onSuccess {
case v => running.decrementAndGet()
}
running.incrementAndGet()
}
}
Time.waitFor(5.0, 0.5) {
val r = running.get()
println(s"Running: $r")
r == 0
}
}
}
}
}
object AsyncDatastore extends H2Datastore(mode = H2Memory("async_test")) {
object users extends Table("users") {
val id = column[Int]("id", PrimaryKey, AutoIncrement)
val name = column[String]("name", NotNull)
val age = column[Int]("age", NotNull)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment