Skip to content

Commit

Permalink
Update unit testing patterns (#731)
Browse files Browse the repository at this point in the history
* Expand test debugging (pretty)

Pretty printing wasn't awesome, but now it's better.

* Move Options into a proper class

* Move message creation into method

This ensures that there's no collisions between tests if we modify properties/dict values

* Use deepcopy on WorkList creation in each test

This ensures that they're always unique, and have no collisions between tests

* Fix broken retry tests

* Add Note to top of retry "steps" test

It's disabled, but can serve as good documentation of how to do it.

* Capture test log existance, and assert against that

It used to assert inside the "if" statement, but that would only ever be True. This way, it would be possible for the logs to be missing, and the test will fail.

* Fix redisqueue cleanup test

No longer need to specify a fake server, becasue we're checking the keys a little more inteligently.

* Clean up commented code

* Expand test debugging (pretty)

Pretty printing wasn't awesome, but now it's better.

* Move Options into a proper class

* Move message creation into method

This ensures that there's no collisions between tests if we modify properties/dict values

* Use deepcopy on WorkList creation in each test

This ensures that they're always unique, and have no collisions between tests

* Fix broken retry tests

* Add Note to top of retry "steps" test

It's disabled, but can serve as good documentation of how to do it.

* Capture test log existance, and assert against that

It used to assert inside the "if" statement, but that would only ever be True. This way, it would be possible for the logs to be missing, and the test will fail.

* Fix redisqueue cleanup test

No longer need to specify a fake server, becasue we're checking the keys a little more inteligently.

* Clean up commented code
  • Loading branch information
gcglinton authored Jul 26, 2023
1 parent e401ab3 commit 10d730a
Show file tree
Hide file tree
Showing 9 changed files with 403 additions and 239 deletions.
133 changes: 93 additions & 40 deletions tests/sarracenia/diskqueue_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,112 @@
import os

#useful for debugging tests
#import pprint
#pretty = pprint.PrettyPrinter(indent=2, width=200).pprint
import pprint
def pretty(*things, **named_things):
for t in things:
pprint.PrettyPrinter(indent=2, width=200).pprint(t)
for k,v in named_things.items():
print(str(k) + ":")
pprint.PrettyPrinter(indent=2, width=200).pprint(v)

from sarracenia.diskqueue import DiskQueue
from sarracenia import Message as SR3Message

class Options:
def __init__(self):
self.no = 1
self.retry_ttl = 0
self.logLevel = "DEBUG"
self.logFormat = ""
self.queueName = "TEST_QUEUE_NAME"
self.component = "sarra"
self.retry_driver = 'disk'
self.redisqueue_serverurl = "redis://Never.Going.To.Resolve:6379/0"
self.config = "foobar.conf"
self.pid_filename = "/tmp/sarracenia/diskqueue_test/pid_filename"
self.housekeeping = float(39)
self.batch = 0
def add_option(self, option, type, default = None):
if not hasattr(self, option):
setattr(self, option, default)

pass

BaseOptions = Options()
BaseOptions.retry_ttl = 0
BaseOptions.logLevel = "DEBUG"
BaseOptions.queueName = "TEST_QUEUE_NAME"
BaseOptions.component = "sarra"
BaseOptions.config = "foobar.conf"
BaseOptions.pid_filename = "/tmp/sarracenia/diskqueue_test/pid_filename"
BaseOptions.housekeeping = float(39)

message = {
"pubTime": "20180118151049.356378078",
"topic": "v02.post.sent_by_tsource2send",
"headers": {
"atime": "20180118151049.356378078",
"from_cluster": "localhost",
"mode": "644",
"mtime": "20180118151048",
"parts": "1,69,1,0,0",
"source": "tsource",
"sum": "d,c35f14e247931c3185d5dc69c5cd543e",
"to_clusters": "localhost"
},
"baseUrl": "https://NotARealURL",
"relPath": "ThisIsAPath/To/A/File.txt",
"notice": "20180118151050.45 ftp://anonymous@localhost:2121 /sent_by_tsource2send/SXAK50_KWAL_181510___58785"
}

def make_message():
m = SR3Message()
m["pubTime"] = "20180118151049.356378078"
m["topic"] = "v02.post.sent_by_tsource2send"
m["mtime"] = "20180118151048"
m["headers"] = {
"atime": "20180118151049.356378078",
"from_cluster": "localhost",
"mode": "644",
"parts": "1,69,1,0,0",
"source": "tsource",
"sum": "d,c35f14e247931c3185d5dc69c5cd543e",
"to_clusters": "localhost"
}
m["baseUrl"] = "https://NotARealURL"
m["relPath"] = "ThisIsAPath/To/A/File.txt"
m["notice"] = "20180118151050.45 ftp://anonymous@localhost:2121 /sent_by_tsource2send/SXAK50_KWAL_181510___58785"
m["_deleteOnPost"] = set()
return m

def test_msgFromJSON(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'work_retry')

message = make_message()

assert message == download_retry.msgFromJSON(jsonpickle.encode(message))

def test_msgToJSON(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'work_retry')

message = make_message()

assert jsonpickle.encode(message) + '\n' == download_retry.msgToJSON(message)

def test__is_exired__TooSoon(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
BaseOptions.retry_ttl = 100000
download_retry = DiskQueue(BaseOptions, 'work_retry')

message = make_message()

assert download_retry.is_expired(message) == True

def test__is_exired__TooLate(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
BaseOptions.retry_ttl = 1
download_retry = DiskQueue(BaseOptions, 'work_retry')

import sarracenia
message = make_message()
message["pubTime"] = sarracenia.nowstr()

assert download_retry.is_expired(message) == False

def test___len__(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'work_retry')

# fp = open(download_retry.queue_file, 'a')
# fp_new = open(download_retry.new_path, 'a')
# fp_hk = open(download_retry.housekeeping_path, 'a')

# fp_new.write(download_retry.msgToJSON(message))
download_retry.msg_count += 1
assert len(download_retry) == 1

download_retry.msg_count_new += 1
assert len(download_retry) == 2

def test_in_cache(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'work_retry')

message = make_message()
download_retry.retry_cache = {}

assert download_retry.in_cache(message) == False
Expand All @@ -97,9 +116,11 @@ def test_in_cache(tmp_path):
assert download_retry.in_cache(message) == True

def test_needs_requeuing(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'work_retry')

message = make_message()
download_retry.retry_cache = {}

assert download_retry.needs_requeuing(message) == True
Expand All @@ -110,9 +131,11 @@ def test_needs_requeuing(tmp_path):
assert download_retry.needs_requeuing(message) == False

def test_put__Single(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'test_put__Single')

message = make_message()
download_retry.put([message])
assert download_retry.msg_count_new == 1

Expand All @@ -121,9 +144,11 @@ def test_put__Single(tmp_path):
assert open(download_retry.new_path, 'r').read() == line

def test_put__Multi(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'test_put__Multi')

message = make_message()
download_retry.put([message, message, message])
assert download_retry.msg_count_new == 3

Expand All @@ -134,9 +159,11 @@ def test_put__Multi(tmp_path):
assert contents == line + line + line

def test_cleanup(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'test_cleanup')

message = make_message()
fp = open(download_retry.queue_file, 'a')
fp.write(jsonpickle.encode(message) + '\n')
download_retry.msg_count = 1
Expand All @@ -150,6 +177,7 @@ def test_cleanup(tmp_path):
assert download_retry.msg_count == 0

def test_msg_get_from_file__NoLine(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'test_msg_get_from_file__NoLine')

Expand All @@ -159,9 +187,12 @@ def test_msg_get_from_file__NoLine(tmp_path):
assert msg == None

def test_msg_get_from_file(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'test_msg_get_from_file')

message = make_message()

fp = open(download_retry.queue_file, 'a')
fp.write(jsonpickle.encode(message) + '\n')
fp.flush()
Expand All @@ -174,9 +205,12 @@ def test_msg_get_from_file(tmp_path):
assert msg == message

def test_get__Single(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'test_get__Single')

message = make_message()

fp = open(download_retry.queue_file, 'a')
line = jsonpickle.encode(message) + '\n'
fp.write(line)
Expand All @@ -189,9 +223,12 @@ def test_get__Single(tmp_path):
assert gotten == [message]

def test_get__Multi(tmp_path):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'test_get__Multi')

message = make_message()

fp = open(download_retry.queue_file, 'a')
line = jsonpickle.encode(message) + '\n'
fp.write(line + line)
Expand All @@ -204,9 +241,12 @@ def test_get__Multi(tmp_path):
assert gotten == [message, message]

def test_on_housekeeping__FinishRetry(tmp_path, caplog):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'test_on_housekeeping__FinishRetry')

message = make_message()

download_retry.queue_fp = open(download_retry.queue_file, 'a')
line = jsonpickle.encode(message) + '\n'
download_retry.queue_fp.write(line + line)
Expand All @@ -216,14 +256,21 @@ def test_on_housekeeping__FinishRetry(tmp_path, caplog):

assert hk_out == None

log_found_notFinished = False

for record in caplog.records:
if "have not finished retry list" in record.message:
assert "have not finished retry list" in record.message
log_found_notFinished = True

assert log_found_notFinished == True

def test_on_housekeeping(tmp_path, caplog):
BaseOptions = Options()
BaseOptions.pid_filename = str(tmp_path) + os.sep + "pidfilename.txt"
download_retry = DiskQueue(BaseOptions, 'test_on_housekeeping')

message = make_message()

download_retry.new_fp = open(download_retry.new_path, 'a')
line = jsonpickle.encode(message) + '\n'
download_retry.new_fp.write(line + line)
Expand All @@ -235,10 +282,16 @@ def test_on_housekeeping(tmp_path, caplog):
assert os.path.exists(download_retry.queue_file) == True
assert os.path.exists(download_retry.new_path) == False

log_found_HasQueue = log_found_NumMessages = log_found_Elapsed = False

for record in caplog.records:
if "has queue" in record.message:
assert "has queue" in record.message
log_found_HasQueue = True
if "Number of messages in retry list" in record.message:
assert "Number of messages in retry list" in record.message
log_found_NumMessages = True
if "on_housekeeping elapse" in record.message:
assert "on_housekeeping elapse" in record.message
log_found_Elapsed = True

assert log_found_HasQueue == True
assert log_found_NumMessages == True
assert log_found_Elapsed == True
9 changes: 7 additions & 2 deletions tests/sarracenia/flowcb/nodupe/data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
import os, types, copy

#useful for debugging tests
#import pprint
#pretty = pprint.PrettyPrinter(indent=2, width=200).pprint
import pprint
def pretty(*things, **named_things):
for t in things:
pprint.PrettyPrinter(indent=2, width=200).pprint(t)
for k,v in named_things.items():
print(str(k) + ":")
pprint.PrettyPrinter(indent=2, width=200).pprint(v)

from sarracenia.flowcb.nodupe.data import Data
from sarracenia import Message as SR3Message
Expand Down
9 changes: 7 additions & 2 deletions tests/sarracenia/flowcb/nodupe/disk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
import os, types, copy

#useful for debugging tests
#import pprint
#pretty = pprint.PrettyPrinter(indent=2, width=200).pprint
import pprint
def pretty(*things, **named_things):
for t in things:
pprint.PrettyPrinter(indent=2, width=200).pprint(t)
for k,v in named_things.items():
print(str(k) + ":")
pprint.PrettyPrinter(indent=2, width=200).pprint(v)

from sarracenia.flowcb.nodupe.disk import NoDupe
from sarracenia import Message as SR3Message
Expand Down
9 changes: 7 additions & 2 deletions tests/sarracenia/flowcb/nodupe/name_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
import os, types, copy

#useful for debugging tests
#import pprint
#pretty = pprint.PrettyPrinter(indent=2, width=200).pprint
import pprint
def pretty(*things, **named_things):
for t in things:
pprint.PrettyPrinter(indent=2, width=200).pprint(t)
for k,v in named_things.items():
print(str(k) + ":")
pprint.PrettyPrinter(indent=2, width=200).pprint(v)

from sarracenia.flowcb.nodupe.name import Name
from sarracenia import Message as SR3Message
Expand Down
9 changes: 7 additions & 2 deletions tests/sarracenia/flowcb/nodupe/nodupe_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
import fakeredis

#useful for debugging tests
#import pprint
#pretty = pprint.PrettyPrinter(indent=2, width=200).pprint
import pprint
def pretty(*things, **named_things):
for t in things:
pprint.PrettyPrinter(indent=2, width=200).pprint(t)
for k,v in named_things.items():
print(str(k) + ":")
pprint.PrettyPrinter(indent=2, width=200).pprint(v)

from sarracenia.flowcb.nodupe.redis import NoDupe as NoDupe_Redis
from sarracenia.flowcb.nodupe.disk import NoDupe as NoDupe_Disk
Expand Down
9 changes: 7 additions & 2 deletions tests/sarracenia/flowcb/nodupe/redis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
import fakeredis, urllib.parse

#useful for debugging tests
#import pprint
#pretty = pprint.PrettyPrinter(indent=2, width=200).pprint
import pprint
def pretty(*things, **named_things):
for t in things:
pprint.PrettyPrinter(indent=2, width=200).pprint(t)
for k,v in named_things.items():
print(str(k) + ":")
pprint.PrettyPrinter(indent=2, width=200).pprint(v)

from sarracenia.flowcb.nodupe.redis import NoDupe
from sarracenia import Message as SR3Message
Expand Down
Loading

0 comments on commit 10d730a

Please sign in to comment.