").css({position: 'fixed', top: 0, right: 0, background: 'white', padding: 10}).text(num +" / "+ total +" Verified").prependTo('body');
+ // Add a nifty UI that tells us how many messages have been verified.
+ // FINALLY, tell gun to subscribe to every record
+ // that is is/will be saved to this table.
+ gun.get('test').map().on(function(data, key){
+ // When we get realtime updates to the data,
+ // create or reuse a DIV that we
+ //var el = $('#'+key).length ? $('#'+key) : $('
');
+ // log the data out to, so we can visually see our test.
+ //$(log).append(el.attr('id', key).text(key +": "+ data));
+ // Now, make sure the received data
+ // matches exactly the data we EXPECT
+ if(("Hello world, "+key+"!") === data){
+ // if it does, we can "check off" record
+ // from our verify todo list.
+ check[key] = 0;
+ // Bump the total number of verified items and update the UI.
+ num += 1;
+ report.text(num +" / "+ total +" Verified");
+ }
+ // Scroll down with the logging.
+ //$('body').stop(true).animate({scrollTop: $(log).height()});
+ // This next part is important:
+ if(Gun.obj.map(check, function(still){
+ // IF THERE ARE ANY RECORDS STILL LEFT TO BE VERIFIED
+ if(still){ return true }
+ })){ return } // return, PREVENTING the test from being finished.
+ // IF HOWEVER, every single message
+ // that we EXPECTED to see
+ // has now been seen
+ // then THIS ONE BROWSER PEER (of many peers)
+ // is finally done.
+ test.done();
+ });
+ // But we have to actually tell the browser to save data!
+ var i = 0, to = setTimeout(function go(){
+ // Cool, make a recursive function
+ // that keeps going until we've saved each message.
+ if(env.config.each <= i){
+ clearTimeout(to);
+ return;
+ }
+ to = setTimeout(go, env.config.wait * Math.random()); // add a little jitter.
+ i += 1;
+ var p = env.id + i;
+ // And actually save the data with gun,
+ // as a record added to one big 'test' table.
+ gun.get('test').get(p).put('Hello world, '+ p +'!');
+ }, env.config.wait);
+ }, {i: i += 1, id: id, ids: ids, config: config}));
+ });
+ // YAY! We're finally done.
+ // IF AND ONLY IF
+ // EVERY SINGLE BROWSER
+ // HAS VERIFIED
+ // EVERY OTHER BROWSERS' data.
+ // If they are ALL done, go to the next step.
+ return Promise.all(tests);
+ });
+
+ after("Everything shut down.", function(){
+ // which is to shut down all the browsers.
+ browsers.run(function(){
+ setTimeout(function(){
+ location.reload();
+ }, 15 * 1000);
+ });
+ // And shut down all the servers.
+ return servers.run(function(){
+ process.exit();
+ });
+ });
+})
+// THE END!
+// Congrats, wasn't that epic?
+// Or still confused how a single 200 LOC test file
+// Is running correctness verification tests
+// across an entire distributed system of devices/browsers?
+// Well, jump on https://gitter.im/amark/gun !
+
+// Think adding tests like this to your work place would be bomb awesome?
+// We totally sell PANIC training, licenses, and support!
+// Please reach out to hi@gunDB.io if you are interested
+// in purchasing consulting or services for PANIC.
\ No newline at end of file