Best JavaScript code snippet using wpt
transaction-test.js
Source:transaction-test.js
1'use strict';2const assert = require('bsert');3const fs = require('fs');4const {testdir, rmTreeDir, randomKey} = require('./util/common');5const nurkel = require('..');6const {Proof} = nurkel;7const NULL_HASH = Buffer.alloc(32, 0);8for (const memory of [false, true]) {9describe(`Urkel Transaction (${memory ? 'MemTree' : 'Tree'})`, function () {10 if (!global.gc)11 this.skip();12 let prefix, tree;13 beforeEach(async () => {14 prefix = testdir('tx');15 if (!memory)16 fs.mkdirSync(prefix);17 tree = nurkel.create({ memory, prefix });18 await tree.open();19 });20 afterEach(async () => {21 await tree.close();22 if (fs.existsSync(prefix))23 rmTreeDir(prefix);24 });25 it('should get the root', async () => {26 const txn1 = tree.txn();27 await txn1.open();28 assert.bufferEqual(txn1.txRootHashSync(), NULL_HASH);29 assert.bufferEqual(await txn1.txRootHash(), NULL_HASH);30 });31 it('should insert and get the key (sync)', async () => {32 if (!tree.supportsSync)33 this.skip();34 const txn1 = tree.txn();35 await txn1.open();36 const key1 = randomKey();37 const value = Buffer.from('Hello !');38 assert.strictEqual(txn1.hasSync(key1), false);39 txn1.insertSync(key1, value);40 assert.strictEqual(txn1.hasSync(key1), true);41 const resValue = txn1.getSync(key1);42 assert.bufferEqual(resValue, value);43 txn1.removeSync(key1);44 assert.strictEqual(txn1.hasSync(key1), false);45 assert.strictEqual(txn1.getSync(key1), null);46 await txn1.close();47 });48 it('should insert and get the key', async () => {49 const txn1 = tree.txn();50 await txn1.open();51 const key1 = randomKey();52 const value = Buffer.from('Hello !');53 assert.strictEqual(await txn1.has(key1), false);54 await txn1.insert(key1, value);55 assert.strictEqual(await txn1.has(key1), true);56 const resValue = await txn1.get(key1);57 assert.bufferEqual(resValue, value);58 await txn1.remove(key1);59 assert.strictEqual(await txn1.has(key1), false);60 assert.strictEqual(await txn1.get(key1), null);61 await txn1.close();62 });63 it('should generate proofs', async () => {64 const txn1 = tree.txn();65 await txn1.open();66 const pairs = new Map();67 for (let i = 0; i < 10; i++) {68 const key = randomKey();69 const value = Buffer.from(`Hello ${i}.`);70 pairs.set(key.toString('hex'), value);71 await txn1.insert(key, value);72 }73 for (const keyHex of pairs.keys()) {74 const key = Buffer.from(keyHex, 'hex');75 const proof = await txn1.prove(key);76 assert(Proof.isProof(proof));77 assert(Buffer.isBuffer(proof.raw));78 if (tree.supportsSync) {79 const proofS = txn1.proveSync(key);80 assert.bufferEqual(proof.raw, proofS.raw);81 }82 }83 await txn1.close();84 });85 it('should clear the transaction', async () => {86 const txn1 = tree.txn();87 await txn1.open();88 assert.bufferEqual(txn1.rootHash(), NULL_HASH);89 for (let i = 0; i < 10; i++) {90 const key = randomKey();91 const value = Buffer.from(`Hello ${i}.`);92 await txn1.insert(key, value);93 }94 assert.notBufferEqual(txn1.rootHash(), NULL_HASH);95 await txn1.clear();96 assert.bufferEqual(txn1.rootHash(), NULL_HASH);97 for (let i = 0; i < 10; i++) {98 const key = randomKey();99 const value = Buffer.from(`Hello ${i}.`);100 await txn1.insert(key, value);101 }102 txn1.clearSync();103 assert.bufferEqual(txn1.rootHash(), NULL_HASH);104 });105 it('should inject', async () => {106 // 5 roots with 5 entries107 const ROOTS = 5;108 const ENTRIES = 5;109 const roots = [];110 const entriesByRoot = [];111 const txn = tree.batch();112 await txn.open();113 for (let i = 0; i < ROOTS; i++) {114 const entries = [];115 for (let j = 0; j < ENTRIES; j++) {116 const key = randomKey();117 const value = Buffer.from(`value ${i * 10 + j}.`);118 entries.push([key, value]);119 await txn.insert(key, value);120 }121 roots.push(await txn.commit());122 entriesByRoot.push(entries);123 }124 await txn.close();125 const last = tree.rootHash();126 const snap = tree.snapshot();127 await snap.open();128 for (let i = ROOTS - 1; i >= 0; i--) {129 // go to the past.130 await snap.inject(roots[i]);131 for (const [rootIndex, entries] of entriesByRoot.entries()) {132 if (rootIndex > i) {133 for (const [key] of entries) {134 assert.strictEqual(await snap.has(key), false);135 assert.strictEqual(await snap.get(key), null);136 if (tree.supportsSync) {137 assert.strictEqual(snap.hasSync(key), false);138 assert.strictEqual(snap.getSync(key), null);139 }140 }141 } else {142 for (const [key, value] of entries) {143 assert.strictEqual(await snap.has(key), true);144 assert.bufferEqual(await snap.get(key), value);145 if (tree.supportsSync) {146 assert.strictEqual(snap.hasSync(key), true);147 assert.bufferEqual(snap.getSync(key), value);148 }149 }150 }151 }152 }153 await snap.inject(last);154 for (const [key, value] of entriesByRoot.flat()) {155 assert.strictEqual(await snap.has(key), true);156 assert.bufferEqual(await snap.get(key), value);157 if (tree.supportsSync) {158 assert.strictEqual(snap.hasSync(key), true);159 assert.bufferEqual(snap.getSync(key), value);160 }161 }162 for (let i = ROOTS - 1; i >= 0; i--) {163 // go to the past.164 await snap.inject(roots[i]);165 for (const [rootIndex, entries] of entriesByRoot.entries()) {166 if (rootIndex > i) {167 for (const [key] of entries) {168 assert.strictEqual(await snap.has(key), false);169 assert.strictEqual(await snap.get(key), null);170 if (tree.supportsSync) {171 assert.strictEqual(snap.hasSync(key), false);172 assert.strictEqual(snap.getSync(key), null);173 }174 }175 } else {176 for (const [key, value] of entries) {177 assert.strictEqual(await snap.has(key), true);178 assert.bufferEqual(await snap.get(key), value);179 if (tree.supportsSync) {180 assert.strictEqual(snap.hasSync(key), true);181 assert.bufferEqual(snap.getSync(key), value);182 }183 }184 }185 }186 }187 await snap.inject(last);188 for (const [key, value] of entriesByRoot.flat()) {189 assert.strictEqual(await snap.has(key), true);190 assert.bufferEqual(await snap.get(key), value);191 if (tree.supportsSync) {192 assert.strictEqual(snap.hasSync(key), true);193 assert.bufferEqual(snap.getSync(key), value);194 }195 }196 await snap.close();197 });198});...
transactions.vows.js
Source:transactions.vows.js
1var vows = require("vows"),2 usingClient = require("./utils").usingClient.gen(),3 assert = require("assert"),4 redis = require("../lib/redis");5var sys = require("sys");6vows.describe("Redis Transactions").addBatch({7 'with proper syntax': usingClient({8 topic: function (client) {9 var simultClient = this.simultClient = redis.createClient();10 simultClient.select(6);11 var self = this;12 client.transaction( function () {13 client.rpush("txn1", 1);14 client.rpush("txn1", 2);15 client.rpush("txn1", 3, self.callback);16 });17 },18 'should result in changes': function (err, count) {19 assert.equal(count, 3);20 },21 teardown: function () {22 this.simultClient.close();23 delete this.simultClient;24 }25 }),26 'with proper syntax with multibulk': usingClient({27 topic: function (client) {28 var self = this;29 client.rpush("txn1-a", 1);30 client.rpush("txn1-a", 2);31 client.rpush("txn1-a", 3);32 client.transaction( function () {33 client.lrange("txn1-a", 0, -1, self.callback); 34 });35 },36 'should return the correct list': function (err, list) {37 assert.deepEqual(list, [1,2,3]);38 }39 }),40 'with commands that require special reply interpretation': usingClient({41 topic: function (client) {42 var self = this;43 client.zadd("txn1-b", 1, 1);44 client.zadd("txn1-b", 2, 2);45 client.zadd("txn1-b", 3, 3);46 client.transaction( function () {47 client.zrange("txn1-b", 0, -1, self.callback); 48 });49 },50 'should return the correct result': function (err, list) {51 assert.deepEqual(list, [1, 2, 3]);52 }53 }),54 'with hgetall': usingClient({55 topic: function (client) {56 var self = this;57 client.hmset("txn1-c", {a: 1, b: 2});58 client.transaction( function () {59 client.hget("txn1-c", "a"); 60 client.hget("txn1-c", "b"); 61 client.hgetall("txn1-c", self.callback); 62 });63 },64 'should transform the result just as it would outside a transaction': function (err, hash) {65 assert.deepEqual(hash, {a: 1, b: 2});66 }67 }),68 'nested': usingClient({69 topic: function (client) {70 var self = this;71 client.transaction( function () {72 client.rpush("nested-txn", "a");73 client.transaction( function () {74 client.rpush("nested-txn", "b");75 });76 client.rpush("nested-txn", "c");77 client.rpush("nested-txn", "d", self.callback);78 });79 },80 'should result in changes': function (err, count) {81 assert.equal(count, 4);82 }83 }),84 'with improper syntax': usingClient({85 topic: function (client) {86 client.transaction( function () {87 client.rpush("txn-invalid", 1);88 client.rpush("txn-invalid", 2);89 client.rpush("txn-invalid");90 // simultClient.rpush("txn", 4, function (err, count) {91 // if (err) throw new Error(err);92 // checkEqual(count, 4, "Commands from other clients should fire after a transaction from a competing client");93 // });94 });95 client.exists("txn-invalid", this.callback);96 },97 // Atomicity98 'should roll back the transaction': function (err, result) {99 assert.equal(result, 0);100 }101 // TODO Should throw an error to notify user of failed transaction102 })...
example3-multiple-transactions.js
Source:example3-multiple-transactions.js
1var lmdb = require('./build/Release/node-lmdb');2var env = new lmdb.Env();3env.open({4 // Path to the environment5 path: "./testdata",6 // Maximum number of databases7 maxDbs: 108});9var dbi = env.openDbi({10 name: "mydb3",11 create: true,12 keyIsUint32: true13});14var data;15// Write values16var txn0 = env.beginTxn();17txn0.putString(dbi, 1, "Hello1");18txn0.putString(dbi, 2, "Hello2");19txn0.commit();20console.log("wrote initial values");21// Now mess around with transactions22var txn1 = env.beginTxn({ readOnly: true });23console.log("txn1: started (read only)");24data = txn1.getString(dbi, 1);25console.log("-----", "txn1", 1, data);26var txn2 = env.beginTxn();27console.log("txn2: started");28txn2.putString(dbi, 1, "Ha ha ha");29console.log("txn2: put other value to key 1");30// txn2 sees the new value immediately31data = txn2.getString(dbi, 1);32console.log("-----", "txn2", 1, data);33// txn1 still sees the old value34data = txn1.getString(dbi, 1);35console.log("-----", "txn1", 1, data);36txn2.commit();37console.log("txn2: committed");38// txn1 still sees the old value!39data = txn1.getString(dbi, 1);40console.log("-----", "txn1", 1, data);41txn1.reset();42txn1.renew();43console.log("rxn1: reset+renewed");44// now txn1 sees the new value45data = txn1.getString(dbi, 1);46console.log("-----", "txn1", 1, data);47try {48 console.log("error expected here:");49 // txn1 is readonly, this will throw an exception!50 txn1.putString(dbi, 2, "hööhh");51}52catch (err) {53 console.log(err);54}55txn1.commit();56console.log("txn1: aborted");57dbi.close();...
Using AI Code Generation
1require('wpt1').txn1();2require('wpt1').txn2();3require('wpt2').txn1();4require('wpt2').txn2();5require('wpt3').txn1();6require('wpt3').txn2();7require('wpt4').txn1();8require('wpt4').txn2();9require('wpt5').txn1();10require('wpt5').txn2();11require('wpt6').txn1();12require('wpt6').txn2();13require('wpt7').txn1();14require('wpt7').txn2();15require('wpt8').txn1();16require('wpt8').txn2();17require('wpt9').txn1();18require('wpt9').txn2();19require('wpt10').txn1();20require('wpt10').txn2();21require('wpt11').txn1();22require('wpt11').txn2();23require('wpt12').txn1();24require('wpt12').txn2();
Using AI Code Generation
1var txn1 = require('./wptxns.js').txn1;2var txn2 = require('./wptxns.js').txn2;3var txn3 = require('./wptxns.js').txn3;4var txn4 = require('./wptxns.js').txn4;5var txn5 = require('./wptxns.js').txn5;6var txn6 = require('./wptxns.js').txn6;7var txn7 = require('./wptxns.js').txn7;8var txn8 = require('./wptxns.js').txn8;9var txn9 = require('./wptxns.js').txn9;10var txn10 = require('./wptxns.js').txn10;11var txn11 = require('./wptxns.js').txn11;12var txn12 = require('./wptxns.js').txn12;13var txn13 = require('./wptxns.js').txn13;14var txn14 = require('./wptxns.js').txn14;15var txn15 = require('./wptxns.js').txn15;16var txn16 = require('./wptxns.js').txn16;17var txn17 = require('./wptxns.js').txn17;
Using AI Code Generation
1var wpt2 = require('./wpt2.js');2var txn1 = wpt2.txn1();3txn1();4var txn1 = function() {5 return function() {6 console.log('txn1');7 }8}9exports.txn1 = txn1;10var txn2 = function() {11 return function() {12 console.log('txn2');13 }14}15exports.txn2 = txn2;
Using AI Code Generation
1var wpt = require('wpt');2var txn1 = wpt.txn1;3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var wpt = require('wpt');10var txn2 = wpt.txn2;11 if (err) {12 console.log(err);13 } else {14 console.log(data);15 }16});17var wpt = require('wpt');18var txn3 = wpt.txn3;19 if (err) {20 console.log(err);21 } else {22 console.log(data);23 }24});25var wpt = require('wpt');26var txn4 = wpt.txn4;27 if (err) {28 console.log(err);29 } else {30 console.log(data);31 }32});33var wpt = require('wpt');34var txn5 = wpt.txn5;35 if (err) {36 console.log(err);37 } else {38 console.log(data);39 }40});41var wpt = require('wpt');42var txn6 = wpt.txn6;43 if (err) {44 console.log(err);45 } else {46 console.log(data);47 }48});49var wpt = require('wpt');50var txn7 = wpt.txn7;51 if (err) {
Using AI Code Generation
1var wpt = require('wpt');2var test = new wpt('your key here');3 console.log(data);4});5var wpt = require('wpt');6var test = new wpt('your key here');7 console.log(data);8});9var wpt = require('wpt');10var test = new wpt('your key here');11 console.log(data);12});13var wpt = require('wpt');14var test = new wpt('your key here');15 console.log(data);16});17var wpt = require('wpt');18var test = new wpt('your key here');19 console.log(data);20});21var wpt = require('wpt');22var test = new wpt('your key here');23 console.log(data);24});25var wpt = require('wpt');26var test = new wpt('your key here');27 console.log(data);28});29var wpt = require('wpt');30var test = new wpt('your key here');31 console.log(data);32});33var wpt = require('wpt');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!