Skip to content

Commit

Permalink
Merge pull request #100 from KarelCemus/stopping
Browse files Browse the repository at this point in the history
Fixed RedisConnector stop on lifecycle stop
  • Loading branch information
KarelCemus authored Jun 26, 2017
2 parents 79bc806 + b100238 commit fb09678
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,10 @@ private[ connector ] class RedisConnectorImpl @Inject()( serializer: AkkaSeriali
}

/** stops the actor */
def stop( ): Future[ Unit ] = {
def stop( ): Future[ Unit ] = Future successful {
log.info( "Stopping the redis cache actor ..." )
redis.quit().map[ Unit ] { _ => log.info( "Redis cache stopped." ) }
redis.stop()
log.info( "Redis cache stopped." )
}

// start the connector
Expand Down
38 changes: 32 additions & 6 deletions src/test/scala/play/api/cache/redis/Redis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@ import scala.concurrent.duration._
import scala.language.implicitConversions

import play.api.Application
import play.api.inject.ApplicationLifecycle
import play.api.inject.guice.GuiceApplicationBuilder

import akka.actor.ActorSystem
import akka.util.Timeout
import org.specs2.matcher._
import org.specs2.specification.BeforeAll
import org.specs2.specification._
import redis.RedisClient

/**
* Provides implicits and configuration for redis tests invocation
*/
trait Redis extends EmptyRedis with RedisMatcher {
trait Redis extends EmptyRedis with RedisMatcher with AfterAll {

def injector = Redis.injector

implicit val application: Application = injector.instanceOf[ Application ]

implicit val system: ActorSystem = injector.instanceOf[ ActorSystem ]

def afterAll( ) = {
Redis.close()
}
}

trait Synchronization {
Expand Down Expand Up @@ -93,7 +98,7 @@ object EmptyRedis extends RedisInstance {
def empty( implicit application: Application, system: ActorSystem ): Unit = synchronized {
// execute only once
if ( !executed ) {
redis.flushdb( ).sync
redis.flushdb().sync
executed = true
}
}
Expand All @@ -104,12 +109,33 @@ case class SimpleObject( key: String, value: Int )

object Redis {

val injector = new GuiceApplicationBuilder( )
import java.util.concurrent.atomic.AtomicInteger

private val stopped = new AtomicInteger( 0 )

val allSpecs = List(
classOf[ configuration.ConfigurationSpec ],
classOf[ connector.RediscalaSpec ],
classOf[ connector.RedisConnectorSpec ],
classOf[ impl.AsynchronousCacheSpec ],
classOf[ impl.JavaCacheSpec ],
classOf[ impl.PlayCacheSpec ],
classOf[ impl.RecoveryPolicySpec ],
classOf[ impl.RedisListSpec ],
classOf[ impl.RedisMapSpecs ],
classOf[ impl.RedisSetSpecs ],
classOf[ impl.SynchronousCacheSpec ],
classOf[ RedisComponentsSpecs ]
).size

def close( ): Unit = if ( stopped.incrementAndGet() == allSpecs )
injector.instanceOf[ ApplicationLifecycle ].stop()

val injector = new GuiceApplicationBuilder()
// load required bindings
.bindings( Seq.empty: _* )
// #19 enable Redis module
.bindings( new RedisCacheModule )
// produce a fake application
.injector( )

.injector()
}
4 changes: 2 additions & 2 deletions src/test/scala/play/api/cache/redis/impl/PlayCacheSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.specs2.mutable.Specification
/**
* <p>Test of play.api.cache.CacheApi to verify compatibility with Redis implementation.</p>
*/
object PlayCacheSpec extends Specification with Redis {
class PlayCacheSpec extends Specification with Redis {

private type Cache = play.api.cache.CacheApi

Expand Down Expand Up @@ -56,7 +56,7 @@ object PlayCacheSpec extends Specification with Redis {
}
}

implicit class AccumulatorCache( val cache: Cache ) extends AnyVal {
implicit class AccumulatorCache( cache: Cache ) {
private type Accumulator = AtomicInteger

/** invokes internal getOrElse but it accumulate invocations of orElse clause in the accumulator */
Expand Down

0 comments on commit fb09678

Please sign in to comment.