it('test write service cache ok',function(done){ try{ var splits = testConf.cachepath.split('/'); var prefix = splits.pop(); var dir = splits.join('/'); var pattern = new RegExp('^'+prefix,'i'); var get = fs.readdirSync(dir); for(var i = 0;i<get.length;i++){ if(pattern.test(get[i])){ fs.unlinkSync(dir+'/'+get[i]); } } }catch(e){} var serv = iShare.subscribe(serviceNode,'2.0',function(err){ if(err){ throw new Error(err); } serv.getServiceAll().length.should.eql(3); var get = JSON.parse(fs.readFileSync(testConf.cachepath + CONSTANTS.SEP + process.pid)); var key = JSON.stringify({name:serviceNode,filter:'2.0'}); get['service'][key].length.should.eql(3); done(); }); });
it('test write app cache ok', function(done){ var step = 0; try{ var splits = testConf.cachepath.split('/'); var prefix = splits.pop(); var dir = splits.join('/'); var pattern = new RegExp('^'+prefix,'i'); var get = fs.readdirSync(dir); for(var i = 0;i<get.length;i++){ if(pattern.test(get[i])){ fs.unlinkSync(dir+'/'+get[i]); } } }catch(e){} var setPath = serviceNode + '/testnode1'; var getEvent = iShare.get(setPath, function(err, version, data){ var get = JSON.parse(fs.readFileSync(testConf.cachepath + CONSTANTS.SEP + process.pid)); iShare.set(setPath, 'abcdefg', function(err){}); }); getEvent.on('update', function(version, data){ var get = JSON.parse(fs.readFileSync(testConf.cachepath + CONSTANTS.SEP + process.pid)); zk.a_delete_('/'+CONSTANTS.APP_ROOT+'/'+setPath,1,function(err){ if(step === 0){ done(); } step++; }); }); });
it('test read service cache and zk recover later ok',function(done){ var step = 0; try{ var splits = testConf.cachepath.split('/'); var prefix = splits.pop(); var dir = splits.join('/'); var pattern = new RegExp('^' + prefix + CONSTANTS.SEP, 'i'); var get = fs.readdirSync(dir); for(var i = 0;i<get.length;i++){ if(pattern.test(get[i])){ fs.unlinkSync(dir+'/'+get[i]); } } }catch(e){} //write fake cache file var key = JSON.stringify({name:serviceNode,filter:'2.0'}); var content1 = [{addr:'http://127.0.0.1:9115',meta:{}}]; var cache1 = {'service':{},'app':{}}; cache1['service'][key] = content1; fs.writeFileSync(testConf.cachepath+CONSTANTS.SEP+'1',JSON.stringify(cache1)); var content2 = [{addr:'http://127.0.0.1:9116',meta:{}}]; var cache2 = {'service':{},'app':{}}; cache2['service'][key] = content2; fs.writeFileSync(testConf.cachepath+CONSTANTS.SEP+'2',JSON.stringify(cache2)); var splits = testConf.zookeeper.split(':'); splits.pop(); splits.push('2188/'); var fake = splits.join(':'); //set useless config iShare.setConfig({ zookeeper : fake, username : '', password : '', cachepath : testConf.cachepath }); var serv = iShare.subscribe(serviceNode,'2.0',function(err){ if(err){ throw new Error(err); } //get cache content if(step !== 0){return;} serv.getServiceAll().length.should.eql(2); step++; //set back to normal config iShare.setConfig({ zookeeper : testConf.zookeeper, username : '', password : '', cachepath : testConf.cachepath }); done(); }); });
setTimeout(function(){ fs.writeFileSync(testConf.cachepath + CONSTANTS.SEP + '3', 'for test3'); Cache.cleanOldCache(); var splits = testConf.cachepath.split('/'); var prefix = splits.pop(); var dir = splits.join('/'); var files = fs.readdirSync(dir); var count = 0; var exp = new RegExp("^" + testConf.cachepath.split('/').pop(), 'i'); for(var i = 0;i < files.length; i++){ if(exp.test(files[i])){ count++; } } count.should.eql(1); CONSTANTS.FILE_TIME_INTERVAL = tmp; done(); }, 3000);
it('test read app cache and zk recover later ok', function(done){ try{ var splits = testConf.cachepath.split('/'); var prefix = splits.pop(); var dir = splits.join('/'); var pattern = new RegExp('^'+prefix,'i'); var get = fs.readdirSync(dir); for(var i = 0;i<get.length;i++){ if(pattern.test(get[i])){ fs.unlinkSync(dir+'/'+get[i]); } } }catch(e){} var path = serviceNode + '/testnode1'; var cache = {'service':{},'app':{}}; cache['app'][path] = {version:4,data:'older data'}; fs.writeFileSync(testConf.cachepath+CONSTANTS.SEP+'1',JSON.stringify(cache)); cache = {'service':{},'app':{}}; cache['app'][path] = {version:5,data:'old data'}; fs.writeFileSync(testConf.cachepath+CONSTANTS.SEP+'2',JSON.stringify(cache)); var splits = testConf.zookeeper.split(':'); splits.pop(); splits.push('2188/'); var fake = splits.join(':'); //set useless config iShare.setConfig({ zookeeper : fake, username : '', password : '', cachepath : testConf.cachepath }); iShare.get(path, function(err, version, data){ version.should.eql(5); data.should.eql('old data'); //set back to normal config iShare.setConfig({ zookeeper : testConf.zookeeper, username : '', password : '', cachepath : testConf.cachepath }); done(); }); });