From a3153246cecfffa1441a4532904d4cb5b9302a77 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Mon, 7 Oct 2013 19:03:20 -0500 Subject: [PATCH] Fix the only thing I see that could possibly still cause #285 --- changelog | 7 +++++++ lib/channel.js | 42 +++++++++++++++++-------------------- tests/naokosimulator2013.js | 18 ++++++++++++---- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/changelog b/changelog index 7711aeb4..1a131823 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,10 @@ +Mon Oct 07 19:00 2013 CDT + * lib/channel.js: Rearrange the callback order to prevent database + lookups from racing with the playlist queue. + * tests/naokosimulator2013.js: Flood the first 10 videos all at once. + Should provide better test coverage for race conditions (especially + on localhost where the latency is ~0) + Mon Oct 07 10:02 2013 CDT * lib/channel.js: Fix several cases where an unregistered channel might attempt to make a database call which then fails. diff --git a/lib/channel.js b/lib/channel.js index ec9f425c..30854cf6 100644 --- a/lib/channel.js +++ b/lib/channel.js @@ -1523,37 +1523,33 @@ Channel.prototype.addMedia = function(data, user) { }); }); } else { - self.server.db.getLibraryItem(self.name, data.id, - function (err, item) { + self.plqueue.queue(function (q) { if (self.dead) return; + self.server.db.getLibraryItem(self.name, data.id, + function (err, item) { + if (self.dead) + return; - if (err) { - user.socket.emit("queueFail", { - msg: "Internal error: " + err, - link: $util.formatLink(data.id, data.type) - }); - return; - } - - if (item !== null) { - if (data.maxlength && item.seconds > data.maxlength) { + if (err) { user.socket.emit("queueFail", { - msg: "Media is too long!", - link: $util.formatLink(item.id, item.type) + msg: "Internal error: " + err, + link: $util.formatLink(data.id, data.type) }); return; } - self.plqueue.queue(function (q) { - if (self.dead) + if (item !== null) { + if (data.maxlength && item.seconds > data.maxlength) { + user.socket.emit("queueFail", { + msg: "Media is too long!", + link: $util.formatLink(item.id, item.type) + }); return; + } + afterData.bind(self, q, true)(item); - }); - } else { - self.plqueue.queue(function (q) { - if (self.dead) - return; + } else { self.server.infogetter.getMedia(data.id, data.type, function (e, m) { if (self.dead) @@ -1569,8 +1565,8 @@ Channel.prototype.addMedia = function(data, user) { afterData.bind(self, q, false)(m); }); - }); - } + } + }); }); } }; diff --git a/tests/naokosimulator2013.js b/tests/naokosimulator2013.js index d3eb2fdc..ed5745a9 100644 --- a/tests/naokosimulator2013.js +++ b/tests/naokosimulator2013.js @@ -6,6 +6,8 @@ socket.on('connect', function () { socket.emit('joinChannel', { name: 'test' }); }); +socket.on('login', testAddVideos); + socket.on('queueFail', function (msg) { console.log(msg); }); @@ -21,7 +23,17 @@ function testAddVideos() { ids.push(pl[i].id); } - for (var i = 0; i < ids.length; i++) { + // burst the first 10 + for (var i = 0; i < 10; i++) { + console.log('queue', ids[i]); + socket.emit('queue', { + id: ids[i], + type: 'yt', + pos: 'end' + }); + } + + for (var i = 10; i < ids.length; i++) { (function (i) { setTimeout(function () { console.log('queue', ids[i]); @@ -30,9 +42,7 @@ function testAddVideos() { type: 'yt', pos: 'end' }); - }, 1050 * i); + }, 1050 * (i - 9)); })(i); } } - -testAddVideos();