var util = require('util');
var EventSource = require('eventsource');
var request = require('request');

var maxClientCount = process.env.CLIENT_COUNT;
var token = process.env.TOKEN;
var satelliteUrl = process.env.SATELLITE_URL + '/broadcast';


class Client {
  constructor(url) {
  	this.es = new EventSource(url);
  }
  receiveMessage(message) {
  	return new Promise((resolve, reject) => {
  		this.es.onmessage = (m => {if (m.data == message) resolve();});
  	})
  }
  close() {
    this.es.close();
  }
}

var randomString = function() {
  var crypto = require('crypto')
    , shasum = crypto.createHash('sha1');
   shasum.update(Math.random().toString());
  return shasum.digest('hex');
}

var randomClientCount = function() {
  return getRandomInt(1, maxClientCount);
}

var getRandomInt = function(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}

var startTest = function () {
  var msg = "PONG";
  var channelUrl = satelliteUrl+'/'+randomString();
  var clientCount = randomClientCount();
  var clients = [];
  for (let i=0; i<clientCount; i++) {
    clients.push(new Client(channelUrl));
  }
  Promise.all(clients.map(c => c.receiveMessage(msg)))
  .then(function(response) {
  	console.log("WORKS for "+clientCount+" clients");
  	clients.map(c => c.close());
  }, function(error) {
  	console.log("FAIL for "+clientCount+" clients");
  	clients.map(c => c.close());
  });
  request.post(channelUrl).form({'token':token,'message':msg})
}

setInterval(function(){startTest()}, 1000);

@@ -277,17 +277,18 @@
 rl) %7B%0A  
-%09
+  
 this.es 
@@ -345,17 +345,18 @@
 ge) %7B%0A  
-%09
+  
 return n
@@ -391,17 +391,18 @@
  =%3E %7B%0A  
-%09
+  
 %09this.es
@@ -460,17 +460,18 @@
 );%7D);%0A  
-%09
+  
 %7D)%0A  %7D%0A 
@@ -1204,25 +1204,26 @@
 sponse) %7B%0A  
-%09
+  
 console.log(
@@ -1254,33 +1254,34 @@
 +%22 clients%22);%0A  
-%09
+  
 clients.map(c =%3E
@@ -1318,17 +1318,18 @@
 or) %7B%0A  
-%09
+  
 console.
@@ -1371,17 +1371,18 @@
 ts%22);%0A  
-%09
+  
 clients.

fix indentation