mathieuancelin / play2-couchbase

Couchbase plugin for Play framework 2.2 with reactive API
http://reactivecouchbase.org/
19 stars 8 forks source link

Problem with test #18

Open damageco opened 11 years ago

damageco commented 11 years ago

Hello,

I've got problem while running tests with the plug-in. Here is my code :

package test

import org.specs2.mutable._
import play.api.test._
import play.api.test.Helpers._
import play.api.libs.ws.WS

/**
 * Add your spec here.
 * You can mock out a whole application including requests, plugins etc.
 * For more information, consult the wiki.
 */
class PersonControllerSpec extends Specification {

  "Application" should {

        "return a person object" in {
      running(FakeApplication()) {
        val reponse = route(FakeRequest(GET, "/persons/4bb975e9-ed03-4548-b54f-dd6ad0221901")).get

        status(reponse) must equalTo(OK)
        contentType(reponse) must beSome.which(_ == "application/json")
        contentAsString(reponse) must contain ("david")
      }
    }
  }
}

here is the stacktrace :

2013-09-26 17:53:45.064 INFO com.couchbase.client.CouchbaseConnection:  Added {QA sa=couchserver.lilel.org/127.0.0.1:11210, #Rops=0, #Wops=0, #iq=0, topRop=null, 
topWop=null, toWrite=0, interested=0} to connect queue
2013-09-26 17:53:45.065 INFO com.couchbase.client.ViewConnection:  Added couchserver.lilel.org to connect queue
2013-09-26 17:53:45.065 INFO com.couchbase.client.CouchbaseConnection:  Connection state changed for sun.nio.ch.SelectionKeyImpl@217209e5
2013-09-26 17:53:45.067 INFO com.couchbase.client.CouchbaseClient:  viewmode property isn't defined. Setting viewmode to production mode
2013-09-26 17:53:45.143 INFO com.couchbase.client.CouchbaseConnection:  Shut down Couchbase client
2013-09-26 17:53:45.151 INFO com.couchbase.client.ViewConnection:  Node couchserver.lilel.org has no ops in the queue
2013-09-26 17:53:45.153 INFO com.couchbase.client.ViewNode:  I/O reactor terminated for couchserver.lilel.org
[info] PersonControllerSpec
[info] 
[info] Application should
[error] ! renvoyer un objet personne
[error]     IllegalStateException: Shutting down (MemcachedConnection.java:719)
[error] net.spy.memcached.MemcachedConnection.checkState(MemcachedConnection.java:903)
[error] net.spy.memcached.MemcachedConnection.enqueueOperation(MemcachedConnection.java:719)
[error] net.spy.memcached.MemcachedClient.asyncGet(MemcachedClient.java:853)
[error] net.spy.memcached.MemcachedClient.asyncGet(MemcachedClient.java:866)
[error] org.ancelin.play2.couchbase.ClientWrapper$class.get(ClientWrapper.scala:181)
[error] org.ancelin.play2.couchbase.Couchbase$.get(Couchbase.scala:38)
[error] org.ancelin.play2.couchbase.crud.CouchbaseCrudSource.get(CrudSource.scala:45)
[error] org.ancelin.play2.couchbase.crud.CouchbaseCrudSourceController$$anonfun$get$2.apply(CrudSource.scala:242)
[error] org.ancelin.play2.couchbase.crud.CouchbaseCrudSourceController$$anonfun$get$2.apply(CrudSource.scala:241)
[error] play.api.mvc.ActionBuilder$$anonfun$apply$11.apply(Action.scala:254)
[error] play.api.mvc.ActionBuilder$$anonfun$apply$11.apply(Action.scala:254)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:217)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109)
[error] play.utils.Threads$.withContextClassLoader(Threads.scala:18)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:108)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:106)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2.apply(Action.scala:106)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2.apply(Action.scala:99)
[error] akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:137)
[info]  

Connection to database closed before the test run. Any idea on what append ?

David

arcane86 commented 11 years ago

Hi,

i've got a similar issue :

import org.junit.runner._
import org.specs2.mutable._
import org.specs2.runner._
import play.api.test._
import play.api.test.Helpers._

@RunWith(classOf[JUnitRunner])
class TeddyTest extends Specification {

    "Teddy" should {

        "find an empty collection first" in new WithApplication() {
            val result = route(FakeRequest(GET, "/teddies")).get

            status(result) must equalTo(OK)
        }
    }
}

give me :

[info] TeddyTest
[info] 
[info] Teddy should
[error] ! find an empty collection first
[error]     IllegalStateException: Shutting down (CouchbaseClient.java:898)
[error] com.couchbase.client.ViewConnection.checkState(ViewConnection.java:228)
[error] com.couchbase.client.CouchbaseClient.addOp(CouchbaseClient.java:898)
[error] com.couchbase.client.CouchbaseClient.asyncGetView(CouchbaseClient.java:348)
[error] org.ancelin.play2.couchbase.ClientWrapper$class.view(ClientWrapper.scala:177)
[error] org.ancelin.play2.couchbase.Couchbase$.view(Couchbase.scala:38)
[error] org.ancelin.play2.couchbase.crud.CouchbaseCrudSource.view(CrudSource.scala:126)
[error] org.ancelin.play2.couchbase.crud.CouchbaseCrudSourceController$$anonfun$find$2.apply(CrudSource.scala:271)
[error] org.ancelin.play2.couchbase.crud.CouchbaseCrudSourceController$$anonfun$find$2.apply(CrudSource.scala:268)
[error] play.api.mvc.ActionBuilder$$anon$1.apply(Action.scala:217)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5$$anonfun$apply$6.apply(Action.scala:109)
[error] play.utils.Threads$.withContextClassLoader(Threads.scala:18)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:108)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2$$anonfun$apply$5.apply(Action.scala:106)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2.apply(Action.scala:106)
[error] play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$2.apply(Action.scala:99)
[error] akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:137)

In the running application, it works fine.

No matter, thanks for the plugin :)

mathieuancelin commented 11 years ago

Hi guys,

I don't really understand what's happening here, I'll try to investigate later, but it seems that the following code works (even if your code is supposed to do exactly the same thing) :

class ApplicationSpec extends Specification {
  "Application" should {
    "find an some urls" in {
      running(FakeApplication()) {
        val home = route(FakeRequest(GET, "/urls/")).get
        val content = contentAsString(home)
        status(home) must equalTo(OK)
        contentType(home) must beSome.which(_ == "application/json")
        Json.parse(content).as[JsArray].value.size must equalTo(22)
      }
    }
  }
}
nielsboldt commented 11 years ago

Hi,

I have also observed the above problem, but the problem seems to be related to creating multiple fake applications, for instance

    "generate a new cookie when we do a request with _chk and a tmp cookie set" in {
        running(FakeApplication()) {
            val cookiesAllocated = CookieGenerator.getAllocatedCookies
            val Some(track) = route(FakeRequest(GET, "/track.gif?_chk").withCookies(Cookie("rtttmp","")))
            status(track) must equalTo(OK)
            cookies(track).get("rtt") must beSome
            val Some(rtt) = cookies(track).get("rtt")
            IdEncoder.decodeUserId(rtt.value) must beGreaterThan(0l)
            CookieGenerator.getAllocatedCookies must beEqualTo(cookiesAllocated+1)
        }
    }
    "generate a new cookie when we do a request with idt cookie set" in {
        running(FakeApplication()) {
            val cookiesAllocated = CookieGenerator.getAllocatedCookies
            val Some(track) = route(FakeRequest(GET, "/track.gif").withCookies(Cookie("idt","hghjhgj")))
            status(track) must equalTo(OK)
            cookies(track).get("rtt") must beSome
            cookies(track).get("rtt").get.httpOnly must beFalse
            val Some(rtt) = cookies(track).get("rtt")
            IdEncoder.decodeUserId(rtt.value) must beGreaterThan(0l)
            CookieGenerator.getAllocatedCookies must beEqualTo(cookiesAllocated+1)
        }
    }

My first case passes and my second case fails. When i comment out the first case, the second case passes. So my guess is that some component cannot handle multiple initializations

mathieuancelin commented 11 years ago

Nice catch, I'll investigate around the plugin initialization