Skip to content

Commit

Permalink
Ignore Pings Quicker than MAX_PING
Browse files Browse the repository at this point in the history
  • Loading branch information
super3 committed Aug 25, 2015
1 parent b684e57 commit 7ee9fbf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dataserv/Farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ def ping(self):
"""
farmer = self.lookup()
farmer.last_seen = datetime.utcnow()
db.session.commit()
time_limit = (datetime.utcnow() - farmer.last_seen).seconds <= app.config["MAX_PING"]

if time_limit:
farmer.last_seen = datetime.utcnow()
db.session.commit()
# else just ignore

# TODO: Actually do an audit.
def audit(self):
Expand Down
1 change: 1 addition & 0 deletions dataserv/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
MAX_PING = 15 # seconds
ONLINE_TIME = 15 # minutes
SQLALCHEMY_DATABASE_URI = "sqlite:///dataserv.db"

Expand Down
13 changes: 13 additions & 0 deletions tests/test_Farmer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
import json
import unittest
from time import mktime
Expand Down Expand Up @@ -64,6 +65,18 @@ def test_ping(self):
ping_time = farmer.last_seen
self.assertTrue(register_time < ping_time)

def test_ping_time_limit(self):
farmer = Farmer(addresses["beta"])
farmer.register()

farmer.ping()
time.sleep(2)
farmer.ping()

# should still be around 2
delta_seconds = int((farmer.last_seen - datetime.utcnow()).seconds)
self.assertNotEqual(delta_seconds, 0)

def test_height(self):
farmer = Farmer(addresses["gamma"])
farmer.register()
Expand Down

0 comments on commit 7ee9fbf

Please sign in to comment.