 			
 			(cb) =>
 				@clientB.emit "joinDoc", @doc_id, cb
+
+			(cb) =>
+				FixturesManager.setUpProject {
+					privilegeLevel: "owner"
+					project: {name: "Test Project"}
+				}, (error, {user_id: @user_id_second, project_id: @project_id_second}) => cb()
+
+			(cb) =>
+				FixturesManager.setUpDoc @project_id_second, {@lines, @version, @ops}, (e, {doc_id: @doc_id_second}) =>
+					cb(e)
+
+			(cb) =>
+				@clientC = RealTimeClient.connect()
+				@clientC.on "connectionAccepted", cb
+
+			(cb) =>
+				@clientC.emit "joinProject", {
+					project_id: @project_id_second
+				}, cb
+			(cb) =>
+				@clientC.emit "joinDoc", @doc_id_second, cb
+
+			(cb) =>
+				@clientAUpdates = []
+				@clientA.on "otUpdateApplied", (update) => @clientAUpdates.push(update)
+				@clientBUpdates = []
+				@clientB.on "otUpdateApplied", (update) => @clientBUpdates.push(update)
+				@clientCUpdates = []
+				@clientC.on "otUpdateApplied", (update) => @clientCUpdates.push(update)
+
+				@clientAErrors = []
+				@clientA.on "otUpdateError", (error) => @clientAErrors.push(error)
+				@clientBErrors = []
+				@clientB.on "otUpdateError", (error) => @clientBErrors.push(error)
+				@clientCErrors = []
+				@clientC.on "otUpdateError", (error) => @clientCErrors.push(error)
+				cb()
 		], done
-		
+
+	afterEach () ->
+		@clientA?.disconnect()
+		@clientB?.disconnect()
+		@clientC?.disconnect()
+
 	describe "with an update from clientA", ->
-		before (done) ->
-			@clientAUpdates = []
-			@clientA.on "otUpdateApplied", (update) => @clientAUpdates.push(update)
-			@clientBUpdates = []
-			@clientB.on "otUpdateApplied", (update) => @clientBUpdates.push(update)
-			
+		beforeEach (done) ->
 			@update = {
 				doc_id: @doc_id
 				op:

			@clientAUpdates.should.deep.equal [{
				v: @version, doc: @doc_id
			}]
			
	describe "with an error", ->
		before (done) ->
			@clientAErrors = []
			@clientA.on "otUpdateError", (error) => @clientAErrors.push(error)
			@clientBErrors = []
			@clientB.on "otUpdateError", (error) => @clientBErrors.push(error)
			
			rclient.publish "applied-ops", JSON.stringify({doc_id: @doc_id, error: @error = "something went wrong"})
			setTimeout done, 200 # Give clients time to get message
			
		it "should send the error to both clients", ->
			@clientAErrors.should.deep.equal [@error]
			@clientBErrors.should.deep.equal [@error]
			
		it "should disconnect the clients", ->
			@clientA.socket.connected.should.equal false
			@clientB.socket.connected.should.equal false

rclient = redis.createClient(settings.redis.pubsub)

describe "receiveUpdate", ->
	beforeEach (done) ->
		@lines = ["test", "doc", "lines"]
		@version = 42
		@ops = ["mock", "doc", "ops"]
