test/test_RedisConnection.js
// Helper function to test all the scenarios of compression, off, on, and dual mode
Helper function to test all the scenarios of compression, off, on, and dual mode
Helper function to test all the scenarios of compression off on and dual mode
function runCommonTest(cacheInstancePut, cacheInstanceGet, test, compressionFlag) {
  var bigDeferred = Q.defer()
  var longVal = 'A long string that should be compressed because it is greater than 750 chars'
  for (i = 0; i < 4; i++) {
   longVal = longVal.concat(longVal)
  }
  var longValOn = '@snappy@wAnwPEEgbG9uZyBzdHJpbmcgdGhhdCBzaG91bGQgYmUgY29tcHJlc3NlZCBiZWNhdXNlIGl0IGlzIGdyZWF0ZXIBMChuIDc1MCBjaGFyc/5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAM5MAA=='
  var tinyVal = 'tiny'
  var tinyValOn = '@orig@tiny'
  var nullVal = 'null'
  var undefinedVal = 'undefined'
  var items = [
    {key: 'longValue2', value: longVal},
    {key: 'tinyValue2', value: tinyVal}
  ]
  var testKeys = ['longValue', 'tinyValue', 'longValue2', 'tinyValue2', 'nullValue', 'undefinedValue']
  var expValsWithoutCompression = [longVal, tinyVal, longVal, tinyVal, nullVal, undefinedVal]
  var expValsWithCompression = [longValOn, tinyValOn, longValOn, tinyValOn, nullVal, undefinedVal]
  var expVals = compressionFlag ? expValsWithCompression : expValsWithoutCompression
  var populateSetterFuncs = function() {
    return [
      cacheInstancePut.set('longValue', longVal, 100000),
      cacheInstancePut.set('tinyValue', tinyVal, 100000),
      cacheInstancePut.set('nullValue', null, 100000),
      cacheInstancePut.set('undefinedValue', undefined, 100000),
      cacheInstancePut.mset(items, 100000)
    ]
  }
  var redisClient = redis.createClient(6379, 'localhost')
  redisClient.on('error', function (err) {
     console.log("error event - " + err)
  })
  var destroyRedisClient = function () {
    redisClient.quit()
    delete redisClient
    return bigDeferred.resolve()
  }
  Q.all(populateSetterFuncs()).then(function () {
    var deferred = Q.defer()
    redisClient.mget(testKeys, function(err, value) {
          return deferred.resolve(value)
    })
    return deferred.promise
   }).then(function (vals) {
       test.deepEqual(expVals, vals)
       return Q.resolve()
   }).then(function () { return cacheInstanceGet.get('longValue')
   }).then(function (val) {
       test.equal(longVal, val)
       return cacheInstanceGet.mget(testKeys)
   }).then(function (vals) {
       test.deepEqual(expValsWithoutCompression, vals)
       destroyRedisClient()
   })
   .fail(function (e) {
       console.error(e)
       test.ok(false, e.stack)
       destroyRedisClient()
   })
   return bigDeferred.promise
}
function runCommonTest ( cacheInstancePut , cacheInstanceGet , test , compressionFlag ) { var bigDeferred = Q . defer ( ) var longVal = 'A long string that should be compressed because it is greater than 750 chars' for ( i = 0 ; i < 4 ; i ++ ) { longVal = longVal . concat ( longVal ) } var longValOn = '@snappy@wAnwPEEgbG9uZyBzdHJpbmcgdGhhdCBzaG91bGQgYmUgY29tcHJlc3NlZCBiZWNhdXNlIGl0IGlzIGdyZWF0ZXIBMChuIDc1MCBjaGFyc/5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAP5MAM5MAA==' var tinyVal = 'tiny' var tinyValOn = '@orig@tiny' var nullVal = 'null' var undefinedVal = 'undefined' var items = [ { key : 'longValue2' , value : longVal } , { key : 'tinyValue2' , value : tinyVal } ] var testKeys = [ 'longValue' , 'tinyValue' , 'longValue2' , 'tinyValue2' , 'nullValue' , 'undefinedValue' ] var expValsWithoutCompression = [ longVal , tinyVal , longVal , tinyVal , nullVal , undefinedVal ] var expValsWithCompression = [ longValOn , tinyValOn , longValOn , tinyValOn , nullVal , undefinedVal ] var expVals = compressionFlag ? expValsWithCompression : expValsWithoutCompression var populateSetterFuncs = function ( ) { return [ cacheInstancePut . set ( 'longValue' , longVal , 100000 ) , cacheInstancePut . set ( 'tinyValue' , tinyVal , 100000 ) , cacheInstancePut . set ( 'nullValue' , null , 100000 ) , cacheInstancePut . set ( 'undefinedValue' , undefined , 100000 ) , cacheInstancePut . mset ( items , 100000 ) ] } var redisClient = redis . createClient ( 6379 , 'localhost' ) redisClient . on ( 'error' , function ( err ) { console . log ( "error event - " + err ) } ) var destroyRedisClient = function ( ) { redisClient . quit ( ) delete redisClient return bigDeferred . resolve ( ) } Q . all ( populateSetterFuncs ( ) ) . then ( function ( ) { var deferred = Q . defer ( ) redisClient . mget ( testKeys , function ( err , value ) { return deferred . resolve ( value ) } ) return deferred . promise } ) . then ( function ( vals ) { test . deepEqual ( expVals , vals ) return Q . resolve ( ) } ) . then ( function ( ) { return cacheInstanceGet . get ( 'longValue' ) } ) . then ( function ( val ) { test . equal ( longVal , val ) return cacheInstanceGet . mget ( testKeys ) } ) . then ( function ( vals ) { test . deepEqual ( expValsWithoutCompression , vals ) destroyRedisClient ( ) } ) . fail ( function ( e ) { console . error ( e ) test . ok ( false , e . stack ) destroyRedisClient ( ) } ) return bigDeferred . promise }
Helper function to test all the scenarios of compression, off, on, and dual mode
Helper function to test all the scenarios of compression off on and dual mode
// longVal will have a length of 1180 after the for loop is executed, making it longer than the pivot //Retrieve all items from redis directly to inspect format // confirm cache entries look good // confirm get works // confirm mget works