Skip to content

Commit

Permalink
Fix: threads used in testing don't close their DB connections (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Dickinson authored Feb 16, 2024
1 parent 5783c00 commit da73960
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/meshapi/tests/test_join_form.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import threading
import time
from unittest import mock

Expand All @@ -11,6 +10,7 @@

from .sample_data import sample_building
from .sample_join_form_data import *
from .util import TestThread

# Grab a reference to the original Install.__init__ function, so that when it gets mocked
# we can still use it when we need it
Expand Down Expand Up @@ -293,9 +293,9 @@ def invoke_join_form(submission, results):
response = self.c.post("/api/v1/join/", request, content_type="application/json")
results.append(response)

t1 = threading.Thread(target=invoke_join_form, args=(member1_submission, results))
t1 = TestThread(target=invoke_join_form, args=(member1_submission, results))
time.sleep(0.5) # Sleep to give the first thread a head start
t2 = threading.Thread(target=invoke_join_form, args=(member2_submission, results))
t2 = TestThread(target=invoke_join_form, args=(member2_submission, results))

t1.start()
t2.start()
Expand Down
10 changes: 5 additions & 5 deletions src/meshapi/tests/test_nn.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import threading
import time
from unittest import mock

Expand All @@ -12,6 +11,7 @@
from ..views import get_next_available_network_number
from .group_helpers import create_groups
from .sample_data import sample_building, sample_install, sample_member
from .util import TestThread


# Test basic NN form stuff (input validation, etc)
Expand Down Expand Up @@ -361,8 +361,8 @@ def invoke_nn_form(install_num: int, outputs_dict: dict):
)
outputs_dict[install_num] = result

t1 = threading.Thread(target=invoke_nn_form, args=(self.install_number1, outputs_dict))
t2 = threading.Thread(target=invoke_nn_form, args=(self.install_number2, outputs_dict))
t1 = TestThread(target=invoke_nn_form, args=(self.install_number1, outputs_dict))
t2 = TestThread(target=invoke_nn_form, args=(self.install_number2, outputs_dict))

t1.start()
t2.start()
Expand Down Expand Up @@ -411,8 +411,8 @@ def invoke_nn_form(install_num: int, outputs: list):
)
outputs.append(result)

t1 = threading.Thread(target=invoke_nn_form, args=(self.install_number1, outputs))
t2 = threading.Thread(target=invoke_nn_form, args=(self.install_number1, outputs))
t1 = TestThread(target=invoke_nn_form, args=(self.install_number1, outputs))
t2 = TestThread(target=invoke_nn_form, args=(self.install_number1, outputs))

t1.start()
t2.start()
Expand Down
10 changes: 10 additions & 0 deletions src/meshapi/tests/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from threading import Thread

from django.db import connection


class TestThread(Thread):
def run(self):
super().run()

connection.close()

0 comments on commit da73960

Please sign in to comment.