Best JavaScript code snippet using mountebank
FrameRateMonitorSpec.js
Source:FrameRateMonitorSpec.js
...27 monitor = undefined;28 }29 });3031 function spinWait(milliseconds) {32 /*jshint noempty: false*/33 var endTime = getTimestamp() + milliseconds;34 while (getTimestamp() < endTime) {35 }36 }3738 it('throws when constructed without a scene', function() {39 expect(function() {40 monitor = new FrameRateMonitor();41 }).toThrowDeveloperError();4243 expect(function() {44 monitor = new FrameRateMonitor({});45 }).toThrowDeveloperError();46 });4748 it('can be constructed with just a scene', function() {49 monitor = new FrameRateMonitor({50 scene : scene51 });5253 expect(monitor.samplingWindow).toBe(5.0);54 expect(monitor.quietPeriod).toBe(2.0);55 expect(monitor.warmupPeriod).toBe(5.0);56 expect(monitor.minimumFrameRateDuringWarmup).toBe(4);57 expect(monitor.minimumFrameRateAfterWarmup).toBe(8);58 expect(monitor.scene).toBe(scene);59 expect(monitor.lowFrameRate.numberOfListeners).toBe(0);60 expect(monitor.nominalFrameRate.numberOfListeners).toBe(0);61 });6263 it('honors parameters to the constructor', function() {64 monitor = new FrameRateMonitor({65 scene : scene,66 samplingWindow : 3.0,67 quietPeriod : 1.0,68 warmupPeriod : 6.0,69 minimumFrameRateDuringWarmup : 1,70 minimumFrameRateAfterWarmup : 271 });7273 expect(monitor.samplingWindow).toBe(3.0);74 expect(monitor.quietPeriod).toBe(1.0);75 expect(monitor.warmupPeriod).toBe(6.0);76 expect(monitor.minimumFrameRateDuringWarmup).toBe(1);77 expect(monitor.minimumFrameRateAfterWarmup).toBe(2);78 expect(monitor.scene).toBe(scene);79 });8081 it('raises the lowFrameRate event on low frame rate', function() {82 monitor = new FrameRateMonitor({83 scene : scene,84 quietPeriod : 0.001,85 warmupPeriod : 0.001,86 samplingWindow : 0.001,87 minimumFrameRateDuringWarmup : 1000,88 minimumFrameRateAfterWarmup : 100089 });9091 var spyListener = jasmine.createSpy('listener');92 monitor.lowFrameRate.addEventListener(spyListener);9394 // Rendering once starts the quiet period95 scene.render();9697 // Wait until we're well past the end of the quiet period.98 spinWait(2);99100 // Rendering again records our first sample.101 scene.render();102103 // Wait well over a millisecond, which is the maximum frame time allowed by this instance.104 spinWait(2);105106 // Record our second sample. The monitor should notice that our frame rate is too low.107 scene.render();108109 expect(monitor.lastFramesPerSecond).toBeLessThan(1000);110 expect(spyListener).toHaveBeenCalled();111 });112113 it('does not monitor frame rate while paused', function() {114 monitor = new FrameRateMonitor({115 scene : scene,116 quietPeriod : 0.001,117 warmupPeriod : 0.001,118 samplingWindow : 0.001,119 minimumFrameRateDuringWarmup : 1000,120 minimumFrameRateAfterWarmup : 1000121 });122123 var spyListener = jasmine.createSpy('listener');124 monitor.lowFrameRate.addEventListener(spyListener);125126 // Rendering once starts the quiet period127 scene.render();128129 // Wait until we're well past the end of the quiet period.130 spinWait(2);131132 // Rendering again records our first sample.133 scene.render();134135 monitor.pause();136137 // Wait well over a millisecond, which is the maximum frame time allowed by this instance.138 spinWait(2);139140 // Record our second sample. The monitor would notice that our frame rate is too low,141 // but it's paused.142 scene.render();143144 monitor.unpause();145146 scene.render();147148 expect(spyListener).not.toHaveBeenCalled();149 });150151 it('pausing multiple times requires unpausing multiple times', function() {152 monitor = new FrameRateMonitor({153 scene : scene,154 quietPeriod : 0.001,155 warmupPeriod : 0.001,156 samplingWindow : 0.001,157 minimumFrameRateDuringWarmup : 1000,158 minimumFrameRateAfterWarmup : 1000159 });160161 var spyListener = jasmine.createSpy('listener');162 monitor.lowFrameRate.addEventListener(spyListener);163164 monitor.pause();165 monitor.pause();166 monitor.unpause();167168 // Rendering once starts the quiet period169 scene.render();170171 // Wait until we're well past the end of the quiet period.172 spinWait(2);173174 // Rendering again records our first sample.175 scene.render();176177 // Wait well over a millisecond, which is the maximum frame time allowed by this instance.178 spinWait(2);179180 // Record our second sample. The monitor would notice that our frame rate is too low,181 // but it's paused.182 scene.render();183184 monitor.unpause();185186 scene.render();187188 expect(spyListener).not.toHaveBeenCalled();189 });190191 it('does not report a low frame rate during the quiet period', function() {192 monitor = new FrameRateMonitor({193 scene : scene,194 quietPeriod : 1.0,195 warmupPeriod : 0.001,196 samplingWindow : 0.001,197 minimumFrameRateDuringWarmup : 1000,198 minimumFrameRateAfterWarmup : 1000199 });200201 var spyListener = jasmine.createSpy('listener');202 monitor.lowFrameRate.addEventListener(spyListener);203204 // Rendering once starts the quiet period205 scene.render();206207 // Wait well over a millisecond, which is the maximum frame time allowed by this instance.208 spinWait(2);209210 // Render again. Even though our frame rate is too low, the monitor shouldn't raise the event because we're in the quiet period.211 scene.render();212213 expect(spyListener).not.toHaveBeenCalled();214 });215216 it('the nominalFrameRate event is raised after the warmup period if the frame rate returns to nominal', function() {217 monitor = new FrameRateMonitor({218 scene : scene,219 quietPeriod : 0.001,220 warmupPeriod : 0.001,221 samplingWindow : 0.001,222 minimumFrameRateDuringWarmup : 10,223 minimumFrameRateAfterWarmup : 10224 });225226 var lowListener = jasmine.createSpy('lowFrameRate');227 monitor.lowFrameRate.addEventListener(lowListener);228229 var nominalListener = jasmine.createSpy('nominalFrameRate');230 monitor.nominalFrameRate.addEventListener(nominalListener);231232 // Rendering once starts the quiet period233 scene.render();234235 // Wait until we're well past the end of the quiet period.236 spinWait(2);237238 // Rendering again records our first sample.239 scene.render();240241 // Wait 120 millseconds, which is over the maximum frame time allowed by this instance.242 spinWait(120);243244 // Record our second sample. The monitor should notice that our frame rate is too low.245 scene.render();246247 expect(monitor.lastFramesPerSecond).toBeLessThan(10);248 expect(lowListener).toHaveBeenCalled();249250 // Render as fast as possible for a samplingWindow, quietPeriod, and warmupPeriod.251 var endTime = getTimestamp() + 50;252 while (getTimestamp() < endTime) {253 scene.render();254 }255256 // The nominalFrameRate event should have been raised.
...
PerformanceWatchdogViewModelSpec.js
Source:PerformanceWatchdogViewModelSpec.js
...25 viewModel = undefined;26 }27 FrameRateMonitor.fromScene(scene).destroy();28 });29 function spinWait(milliseconds) {30 /*eslint-disable no-empty*/31 var endTime = getTimestamp() + milliseconds;32 while (getTimestamp() < endTime) {33 }34 /*eslint-enable no-empty*/35 }36 it('throws when constructed without a scene', function() {37 expect(function() {38 viewModel = new PerformanceWatchdogViewModel();39 }).toThrowDeveloperError();40 expect(function() {41 viewModel = new PerformanceWatchdogViewModel({});42 }).toThrowDeveloperError();43 });44 it('can be constructed with just a scene', function() {45 viewModel = new PerformanceWatchdogViewModel({46 scene : scene47 });48 expect(viewModel.lowFrameRateMessage).toBeDefined();49 expect(viewModel.lowFrameRateMessageDismissed).toBe(false);50 expect(viewModel.showingLowFrameRateMessage).toBe(false);51 expect(viewModel.scene).toBe(scene);52 });53 it('honors parameters to the constructor', function() {54 var options = {55 scene : scene,56 lowFrameRateMessage : 'why so slow?'57 };58 viewModel = new PerformanceWatchdogViewModel(options);59 expect(viewModel.lowFrameRateMessage).toBe('why so slow?');60 expect(viewModel.scene).toBe(scene);61 });62 it('shows a message on low frame rate', function() {63 var monitor = FrameRateMonitor.fromScene(scene);64 monitor.quietPeriod = 0.001;65 monitor.warmupPeriod = 0.001;66 monitor.samplingWindow = 0.001;67 monitor.minimumFrameRateDuringWarmup = 1000;68 monitor.minimumFrameRateAfterWarmup = 1000;69 viewModel = new PerformanceWatchdogViewModel({70 scene : scene71 });72 expect(viewModel.showingLowFrameRateMessage).toBe(false);73 // Rendering once starts the quiet period74 scene.render();75 // Wait until we're well past the end of the quiet period.76 spinWait(2);77 // Rendering again records our first sample.78 scene.render();79 // Wait well over a millisecond, which is the maximum frame time allowed by this instance.80 spinWait(2);81 // Record our second sample. The watchdog should notice that our frame rate is too low.82 scene.render();83 expect(viewModel.showingLowFrameRateMessage).toBe(true);84 });85 it('does not report a low frame rate during the queit period', function() {86 var monitor = FrameRateMonitor.fromScene(scene);87 monitor.quietPeriod = 1.0;88 monitor.warmupPeriod = 0.001;89 monitor.samplingWindow = 0.001;90 monitor.minimumFrameRateDuringWarmup = 1000;91 monitor.minimumFrameRateAfterWarmup = 1000;92 viewModel = new PerformanceWatchdogViewModel({93 scene : scene94 });95 // Rendering once starts the quiet period96 scene.render();97 // Wait well over a millisecond, which is the maximum frame time allowed by this instance.98 spinWait(2);99 // Render again. Even though our frame rate is too low, the watchdog shouldn't bark because we're in the quiet period.100 scene.render();101 expect(viewModel.showingLowFrameRateMessage).toBe(false);102 });103 it('the low frame rate message goes away after the warmup period if the frame rate returns to nominal', function() {104 var monitor = FrameRateMonitor.fromScene(scene);105 monitor.quietPeriod = 0.001;106 monitor.warmupPeriod = 0.001;107 monitor.samplingWindow = 0.001;108 monitor.minimumFrameRateDuringWarmup = 10;109 monitor.minimumFrameRateAfterWarmup = 10;110 viewModel = new PerformanceWatchdogViewModel({111 scene : scene112 });113 expect(viewModel.showingLowFrameRateMessage).toBe(false);114 // Rendering once starts the quiet period115 scene.render();116 // Wait until we're well past the end of the quiet period.117 spinWait(2);118 // Rendering again records our first sample.119 scene.render();120 // Wait 120 millseconds, which is over the maximum frame time allowed by this instance.121 spinWait(120);122 // Record our second sample. The watchdog should notice that our frame rate is too low.123 scene.render();124 expect(viewModel.showingLowFrameRateMessage).toBe(true);125 // Render as fast as possible for a samplingWindow, quietPeriod, and warmupPeriod.126 var endTime = getTimestamp() + 50;127 while (getTimestamp() < endTime) {128 scene.render();129 }130 // The low frame rate message should have gone away.131 expect(viewModel.showingLowFrameRateMessage).toBe(false);132 });133 it('does not show the low frame rate message again once it is dismissed', function() {134 var monitor = FrameRateMonitor.fromScene(scene);135 monitor.quietPeriod = 0.001;136 monitor.warmupPeriod = 0.001;137 monitor.samplingWindow = 0.001;138 monitor.minimumFrameRateDuringWarmup = 1000;139 monitor.minimumFrameRateAfterWarmup = 1000;140 viewModel = new PerformanceWatchdogViewModel({141 scene : scene142 });143 expect(viewModel.showingLowFrameRateMessage).toBe(false);144 // Rendering once starts the quiet period145 scene.render();146 // Wait until we're well past the end of the quiet period.147 spinWait(2);148 // Rendering again records our first sample.149 scene.render();150 // Wait well over a millisecond, which is the maximum frame time allowed by this instance.151 spinWait(2);152 // Record our second sample. The watchdog should notice that our frame rate is too low.153 scene.render();154 expect(viewModel.showingLowFrameRateMessage).toBe(true);155 viewModel.dismissMessage();156 // Render several slow frames. The message should not re-appear.157 scene.render();158 spinWait(2);159 scene.render();160 expect(viewModel.showingLowFrameRateMessage).toBe(false);161 });...
Using AI Code Generation
1const mb = require('mountebank');2mb.start({3})4 .then(() => {5 console.log("Mountebank started");6 return mb.post('/imposters', {7 {8 {9 is: {10 headers: {11 },12 body: JSON.stringify({ message: 'Hello World!' })13 }14 }15 }16 });17 })18 .then(() => {19 console.log("Imposter created");20 return mb.get('/imposters');21 })22 .then(response => {23 console.log("Imposter retrieved");24 console.log(response.body);25 })26 .then(() => {27 console.log("Stopping mountebank");28 return mb.stop();29 })30 .then(() => {31 console.log("Mountebank stopped");32 })33 .catch(error => {34 console.error(error);35 });
Using AI Code Generation
1var mb = require('mountebank');2mb.create({3}, function (error) {4 if (error) {5 console.error(error);6 process.exit(1);7 }8 console.log('Server is listening on port 2525');9});10mb.post('/imposters', {11 {12 {13 is: {14 }15 }16 }17}, function (error, response) {18 if (error) {19 console.error(error);20 process.exit(1);21 }22 console.log('Stub created on port 4545');23});24mb.spinWait(function () {25 console.log('Server is ready');26});27mb.del('/imposters/4545', function (error, response) {28 if (error) {29 console.error(error);30 process.exit(1);31 }32 console.log('Stub deleted');33});34mb.del('/imposters', function (error, response) {35 if (error) {36 console.error(error);37 process.exit(1);38 }39 console.log('Server deleted');40});41mb.spinWait(function () {42 console.log('Server is down');43});44mb.del('/imposters', function (error, response) {45 if (error) {46 console.error(error);47 process.exit(1);48 }49 console.log('Server deleted');50});51mb.spinWait(function () {52 console.log('Server is down');53});54mb.del('/imposters', function (error, response) {55 if (error) {56 console.error(error);57 process.exit(1);58 }59 console.log('Server deleted');60});61mb.spinWait(function () {62 console.log('Server is down');63});64mb.del('/imposters', function (error, response) {65 if (error)
Using AI Code Generation
1const mb = require('mountebank');2const imposter = mb.create();3imposter.port = 3000;4imposter.protocol = 'http';5imposter.stub = {6 responses: [{ is: { body: 'Hello World!' } }]7};8imposter.addStub();9imposter.addStub({10 responses: [{ is: { body: 'Hello World 2!' } }]11});12imposter.save().then(() => {13 console.log('imposter saved');14 imposter.start().then(() => {15 console.log('imposter started');16 console.log('waiting for imposter to start');17 imposter.spinWait(100, 2000).then(() => {18 console.log('imposter started');19 });20 });21});22const mb = require('mountebank');23const imposter = mb.create();24imposter.port = 3000;25imposter.protocol = 'http';26imposter.stub = {27 responses: [{ is: { body: 'Hello World!' } }]28};29imposter.addStub();30imposter.addStub({31 responses: [{ is: { body: 'Hello World 2!' } }]32});33imposter.save().then(() => {34 console.log('imposter saved');35 imposter.start().then(() => {36 console.log('imposter started');37 console.log('waiting for imposter to start');38 imposter.waitFor(2000).then(() => {39 console.log('imposter started');40 });41 });42});43const mb = require('mountebank');44const imposter = mb.create();45imposter.port = 3000;46imposter.protocol = 'http';47imposter.stub = {48 responses: [{ is: { body: 'Hello World!' } }]49};50imposter.addStub();51imposter.addStub({52 responses: [{ is: { body: 'Hello World 2!' } }]53});54imposter.save().then(() => {55 console.log('imposter saved');56 imposter.start().then(() => {57 console.log('imposter started');58 console.log('waiting for imposter to start');59 imposter.waitFor(2000).then(() => {60 console.log('imposter started');61 });62 });63});
Using AI Code Generation
1var mb = require('mountebank');2var request = require('request');3var Q = require('q');4var promise = mb.create({port: 2525, pidfile: 'mb.pid', logfile: 'mb.log'});5promise.then(function (mb) {6 return mb.post('/imposters', {7 {8 {9 equals: {10 }11 }12 {13 is: {14 }15 }16 }17 });18}).then(function (response) {19 var deferred = Q.defer();20 if (error) {21 deferred.reject(new Error('Error making request: ' + error.message));22 }23 else if (response.statusCode !== 200) {24 deferred.reject(new Error('Received non-200 response: ' + response.statusCode));25 }26 else if (body !== 'Hello, world!') {27 deferred.reject(new Error('Received incorrect response: ' + body));28 }29 else {30 deferred.resolve();31 }32 });33 return deferred.promise;34}).done(function () {35 console.log('Success!');36});
Using AI Code Generation
1var mb = require('mountebank');2var Q = require('q');3var fs = require('fs');4var path = require('path');5var request = require('request');6var assert = require('assert');7var logger = require('winston');8var logger = new (logger.Logger)({9 new (logger.transports.Console)()10});11var port = 2525;12var protocol = 'http';13var host = 'localhost';14var imposters = [{15 stubs: [{16 responses: [{17 is: {18 }19 }]20 }]21}];22var options = {23};24var deferred = Q.defer();25request.post(options, function (error, response, body) {26 if (error) {27 deferred.reject(error);28 } else {29 deferred.resolve(body);30 }31});32.then(function (response) {33 logger.info('Imposter created:', response);34 return mb.spinWait(function () {35 return Q.nfcall(request.get, mbHost + '/imposters/3000');36 }, 500, 10000);37})38.then(function (response) {39 logger.info('Imposter is ready:', response);40 var options = {41 body: [{42 responses: [{43 is: {44 }45 }]46 }]47 };48 var deferred = Q.defer();49 request.post(options, function (error, response, body) {50 if (error) {51 deferred.reject(error);52 } else {53 deferred.resolve(body);54 }55 });56 return deferred.promise;57})58.then(function (response) {59 logger.info('Stub created:', response);60 var options = {61 body: {62 responses: [{63 is: {64 }65 }]66 }67 };
Using AI Code Generation
1var mb = require('mountebank'),2 client = mb.createClient({ port: 2525 }),3 wait = require('wait.for');4console.log(result);5{ port: 2525,6 { self:7 method: 'GET' },8 method: 'GET' } },9 requests: [] }10The function spinWait() takes 3 parameters:11{12 {13 {14 "proxy": {15 }16 }17 }18}
Using AI Code Generation
1var spinWait = require('mountebank/src/util/spinWait');2var port = 2525;3spinWait(port, 1000, 5000).then(function() {4 console.log("Service is up and running");5});6var mb = require('mountebank');7var port = 2525;8mb.spinWait(port, 1000, 5000).then(function() {9 console.log("Service is up and running");10});
Using AI Code Generation
1var mb = require('mountebank');2var mbHelper = require('./mbHelper');3var Q = require('q');4var _ = require('lodash');5var serverUp = mbHelper.spinWait(function () {6 return mb.create();7});8var impostersCreated = serverUp.then(function (server) {9 return mbHelper.spinWait(function () {10 return mb.createImposter(2525, [{ protocol: 'http', port: 8080 }]);11 });12});13var stubsCreated = impostersCreated.then(function (imposters) {14 return mbHelper.spinWait(function () {15 return mb.createStub(2525, 8080, [{ responses: [{ is: { body: 'Hello world!' } }] }]);16 });17});18var serverDown = stubsCreated.then(function (stubs) {19 return mbHelper.spinWait(function () {20 return mb.shutdown(2525);21 });22});23var serverDown = stubsCreated.then(function (stubs) {24 return mbHelper.spinWait(function () {25 return mb.shutdown(2525);26 });27});28var serverDown = stubsCreated.then(function (stubs) {29 return mbHelper.spinWait(function () {30 return mb.shutdown(2525);31 });32});33var serverDown = stubsCreated.then(function (stubs) {34 return mbHelper.spinWait(function () {35 return mb.shutdown(2525);36 });37});38var serverDown = stubsCreated.then(function (stubs) {39 return mbHelper.spinWait(function () {40 return mb.shutdown(2525);41 });42});43var serverDown = stubsCreated.then(function (stubs) {44 return mbHelper.spinWait(function () {45 return mb.shutdown(2525);46 });47});48var serverDown = stubsCreated.then(function (stubs) {
Using AI Code Generation
1{2 {3 {4 "is": {5 "headers": {6 },7 },8 "_behaviors": {9 }10 }11 {12 "equals": {13 }14 }15 }16}17{18 {19 {20 "is": {21 "headers": {22 },23 },24 "_behaviors": {25 }26 }27 {28 "equals": {29 }30 }31 }32}33{34 {35 {36 "is": {37 "headers": {38 },39 },40 "_behaviors": {41 }42 }43 {44 "equals": {45 }46 }47 }48}
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!!