require('./harness').run();

var recvCount = 0;
var body = "hello world";

connection.addListener('ready', function () {
  puts("connected to " + connection.serverProperties.product);

  var e = connection.exchange('node-purge-fanout', {type: 'fanout'});
  var q = connection.queue('node-purge-queue', function() {
    q.bind(e, "*")
    q.on('queueBindOk', function() {
      puts("publishing 1 json messages");
      e.publish('ackmessage.json1', { name: 'A' });

      puts('Purge queue')
      q.purge().addCallback(function(ok){
        puts('Deleted '+ok.messageCount+' messages')
        assert.equal(1,ok.messageCount)
        puts("publishing another json messages");
        e.publish('ackmessage.json2', { name: 'B' });
      })

      q.on('basicConsumeOk', function () {
        setTimeout(function () {
          // wait one second to receive the message, then quit
          connection.end();
        }, 1000);
      });

      q.subscribe({ ack: true }, function (json) {
        recvCount++;
        puts('Got message ' + JSON.stringify(json));
        if (recvCount == 1) {
          assert.equal('B', json.name);
          q.shift();
         } else {
          throw new Error('Too many message!');
         }
      })
    })
  });
});


process.addListener('exit', function () {
  assert.equal(1, recvCount);
});

@@ -244,16 +244,31 @@
 'fanout'
+, confirm: true
 %7D);%0A  va
@@ -341,16 +341,17 @@
 (e, %22*%22)
+;
 %0A    q.o
@@ -413,29 +413,29 @@
 json message
-s
 %22);%0A
+%0A
       e.publ
@@ -471,20 +471,38 @@
 e: 'A' %7D
-);%0A%0A
+, %7B%7D, function() %7B%0A%0A  
       pu
@@ -518,17 +518,20 @@
  queue')
-%0A
+;%0A  
       q.
@@ -568,24 +568,26 @@
 k)%7B%0A        
+  
 puts('Delete
@@ -619,12 +619,14 @@
 sage
-s
 ')
-%0A
+;%0A  
     
@@ -660,25 +660,27 @@
 geCount)
-%0A
+;%0A 
         
 puts(%22pu
@@ -667,24 +667,25 @@
 );%0A         
+ 
 puts(%22publis
@@ -713,13 +713,14 @@
 sage
-s
 %22);%0A
+  
     
@@ -769,24 +769,26 @@
 'B' %7D);%0A
+  
       %7D)
 %0A%0A      
@@ -779,18 +779,21 @@
       %7D)
+;
 %0A%0A
+  
       q.
@@ -819,32 +819,34 @@
 , function () %7B%0A
+  
         setTimeo
@@ -872,16 +872,18 @@
         
+  
 // wait 
@@ -937,16 +937,18 @@
         
+  
 connecti
@@ -953,24 +953,26 @@
 tion.end();%0A
+  
         %7D, 1
@@ -979,29 +979,33 @@
 000);%0A      
+  
 %7D);%0A%0A
+  
       q.subs
@@ -1051,16 +1051,18 @@
         
+ 
+ 
 recvCoun
@@ -1066,16 +1066,18 @@
 ount++;%0A
+  
         
@@ -1129,16 +1129,18 @@
         
+  
 if (recv
@@ -1155,32 +1155,34 @@
  1) %7B%0A          
+ 
+ 
 assert.equal('B'
@@ -1205,16 +1205,18 @@
         
+  
 q.shift(
@@ -1214,24 +1214,26 @@
  q.shift();%0A
+  
          %7D e
@@ -1248,16 +1248,18 @@
         
+  
 throw ne
@@ -1301,18 +1301,22 @@
     
-%7D%0A
+  %7D%0A  
       %7D)
 %0A   
@@ -1315,15 +1315,30 @@
   %7D)
-%0A
+;%0A%0A  
     %7D)
+;%0A%0A%0A    %7D);
 %0A  %7D

Fix race condition in purge test.