       projectedField = Fields.findOne userId
 
       {user, projectedField}
+
+    setLocalCollectionLimit: (limit) ->
+      localCollectionLimit.set limit
+
+    insertLocalCollection: (doc) ->
+      LocalCollection.insert doc
+
+else
+  LocalCollection = new Mongo.Collection 'localCollection'
 
 class ReactivePublishTestCase extends ClassyTestCase
   @testName: 'reactivepublish'


    Posts.find()

  # We use our own insert method to not have latency compensation so that observeChanges
  # on the client really matches how databases changes on the server.
  Meteor.methods
    'insertPost': (timestamp) ->
      check timestamp, Number

      Posts.insert
        timestamp: timestamp

    'userAndProjection': (userId) ->
      user = Users.findOne userId,
        fields:
          posts: 1

      onError: @expect =>
        @assertTrue true

  testClientLocalCollection: [
    ->
      Meteor.call 'setLocalCollectionLimit', 10, @expect (error) =>
          @assertFalse error, error
  ,
    ->
      @assertSubscribeSuccessful 'localCollection', @expect()
  ,
    ->
      @assertEqual LocalCollection.find({}).fetch(), []

      for i in [0...10]
        Meteor.call 'insertLocalCollection', {i: i}, @expect (error, documentId) =>
          @assertFalse error, error
          @assertTrue documentId
  ,
    ->
      # To wait a bit for change to propagate.
      Meteor.setTimeout @expect(), 100 # ms
  ,
    ->
      @assertEqual LocalCollection.find({}).count(), 10

      Meteor.call 'setLocalCollectionLimit', 5, @expect (error) =>
        @assertFalse error, error

      # To wait a bit for change to propagate.
      Meteor.setTimeout @expect(), 100 # ms
  ,
    ->
      @assertEqual LocalCollection.find({}).count(), 5

      for i in [0...10]
        Meteor.call 'insertLocalCollection', {i: i}, @expect (error, documentId) =>
          @assertFalse error, error
          @assertTrue documentId
  ,
    ->
      # To wait a bit for change to propagate.
      Meteor.setTimeout @expect(), 100 # ms
  ,
    ->
      @assertEqual LocalCollection.find({}).count(), 5

      Meteor.call 'setLocalCollectionLimit', 15, @expect (error) =>
        @assertFalse error, error

      # To wait a bit for change to propagate.
      Meteor.setTimeout @expect(), 100 # ms
  ,
    ->
      @assertEqual LocalCollection.find({}).count(), 15
  ]

# Register the test case.
ClassyTestCase.addTest new ReactivePublishTestCase()
