function expectStreamToEmit(count, expectation) { if (typeof count === "function") { expectation = count; count = 1; } var lastfm = new LastFmNode(), connection = new fakes.Client(80, lastfm.host), request = new fakes.LastFmRequest(), gently = new Gently(); gently.expect(lastfm, "request", function() { return request; }); var trackStream = new RecentTracksStream(lastfm, "username"); trackStream.start(); trackStream.stop(); for(var index = 0; index < previousEmits.length; index++) { request.emit("success", previousEmits[index]); } gently.expect(trackStream, "emit", count, expectation); for(var times = 0; times < requestEmits[2]; times++) { request.emit(requestEmits[0], requestEmits[1]); } }
it("starts and stops streaming when requested", function() { gently.expect(lastfm, "request", 1, function(method, params) { return request; }); var trackStream = new RecentTracksStream(lastfm); trackStream.start(); trackStream.stop(); assert.ok(!trackStream.isStreaming()); });
it("queries API every 10 seconds", function() { var trackStream = new RecentTracksStream(lastfm, "username"); var timestamps = []; gently.expect(lastfm, "request", 2, function(method, params) { timestamps.push((new Date).getTime()); if (timestamps.length === 2) { trackStream.stop(); var delay = timestamps[1] - timestamps[0]; assert.ok(delay >= (10000 - 50) && delay <= (10000 + 50)); } return request; }); trackStream.start(); });
it("queries API every 10 seconds", function() { var trackStream = new RecentTracksStream(lastfm, "username"); var count = 0; RecentTracksStream.prototype.scheduleCallback = function(callback, delay) { count++; if (count === 10) { trackStream.stop(); } assert.ok(delay, 10000); gently.expect(lastfm, "request", function(method, params) { return new fakes.LastFmRequest(); }); callback(); }; gently.expect(lastfm, "request", function(method, params) { return new fakes.LastFmRequest(); }); trackStream.start(); });