Permalink
Browse files

more tests. Handle errors correctly

  • Loading branch information...
1 parent 85df1ed commit 3eec220fb3af2d869825a197502ac5f0eb690d42 @Raynos Raynos committed Jan 16, 2013
Showing with 81 additions and 16 deletions.
  1. +1 −1 package.json
  2. +1 −2 subscribe.js
  3. +13 −13 test/duplex.js
  4. +1 −0 test/index.js
  5. +65 −0 test/many-subscribers.js
View
@@ -18,7 +18,7 @@
"email": "raynos2@gmail.com"
},
"dependencies": {
- "read-stream": "~0.5.2",
+ "read-stream": "~1.0.1",
"push-queue": "~0.1.1",
"end-stream": "~0.1.0",
"expiry-model": "~1.0.1",
View
@@ -60,8 +60,7 @@ function subscribe(client, channel, options) {
queue.push(JSON.parse(message))
}
, error: function (info) {
- var message = info[1] || "pubsub subscribe failed"
- stream.emit("error", Error(message))
+ stream.emit("error", Error("pubsub subscribe failed"))
}
})
View
@@ -20,19 +20,19 @@ test("can subscribe to channel", function (assert) {
})
})
-test("can subscribe to two channels", function (assert) {
- var client = connect()
- var done = after(2, function () { assert.end() })
+testConnections(2)
+testConnections(10)
+testConnections(50)
- throughput(duplex(client, channel + "1"), assert, done)
- throughput(duplex(client, channel + "2"), assert, done)
-})
+function testConnections(n) {
+ var id = uuid()
-test("can subscribe to 10 channels", function (assert) {
- var client = connect()
- var done = after(10, function () { assert.end() })
+ test("can subscribe to " + n + " channels", function (assert) {
+ var client = connect()
+ var done = after(n, function () { assert.end() })
- for (var i = 3; i < 13; i++) {
- throughput(duplex(client, channel + i), assert, done)
- }
-})
+ for (var i = 0; i < n; i++) {
+ throughput(duplex(client, channel + id + i), assert, done)
+ }
+ })
+}
View
@@ -1,2 +1,3 @@
require("./duplex")
require("./publish")
+require("./many-subscribers")
@@ -0,0 +1,65 @@
+var test = require("tape")
+var uuid = require("node-uuid")
+var after = require("after")
+
+var connect = require("./util/connect")
+
+var publish = require("../publish")
+var subscribe = require("../subscribe")
+
+var channel = "pubnub stream test many subscribers" + uuid()
+
+test.only("50 concurrent subcriptions to the same channel", function (assert) {
+ var writable = publish(connect(), channel)
+ var list = range(50)
+ var next = after(50, publishMessages)
+ var done = after(50, function () { assert.end() })
+
+ var streams = list.
+ map(function (i) { return subscribe(connect(), channel) }).
+ forEach(function (stream, i) {
+ var chunks = ["one", "two", "three"]
+ stream.once("connect", function () {
+ // console.log("connected", i)
+ next()
+ })
+
+ stream.on("data", function (chunk) {
+ // console.log("got chunk", chunk, i)
+ assert.equal(chunk, chunks.shift())
+
+ if (chunks.length === 0) {
+ stream.close()
+ }
+ })
+
+ stream.on("end", function () {
+ // console.log("END", i)
+ done()
+ })
+ })
+
+ function publishMessages() {
+ // console.log("publishingMessages")
+
+ writable.write("one")
+ writable.write("two")
+ writable.write("three")
+ writable.end()
+ }
+})
+
+function range(start, end) {
+ if (end === undefined) {
+ end = start
+ start = 0
+ }
+
+ var list = []
+
+ for (var i = start; i < end; i++) {
+ list.push(i)
+ }
+
+ return list
+}

0 comments on commit 3eec220

Please sign in to comment.