Skip to content

Commit

Permalink
Merge pull request #4 from BNL-ATF/tst-start-socket-server
Browse files Browse the repository at this point in the history
TST: start `test-socket-server` within the fixture
  • Loading branch information
BriannaRomasky authored Feb 16, 2023
2 parents fb0fad2 + 977159a commit d3cef75
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ jobs:
pip install -r requirements-dev.txt
pip list
- name: Start test socket server
run: |
set -vxeuo pipefail
nohup test-socket-server > /tmp/socket.log 2>&1 &
# - name: Start test socket server
# run: |
# set -vxeuo pipefail
# nohup test-socket-server > /tmp/socket.log 2>&1 &

- name: Test with pytest
run: |
set -vxeuo pipefail
pytest -s -vv
- name: Check socket server logs
run: |
set -vxeuo pipefail
cat /tmp/socket.log
# - name: Check socket server logs
# run: |
# set -vxeuo pipefail
# cat /tmp/socket.log
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/ambv/black
rev: 22.12.0
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
- repo: https://github.com/pycqa/isort
rev: 5.11.2
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/kynan/nbstripout
Expand Down
1 change: 1 addition & 0 deletions atfdb/atfdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ def host_connect(host_name=None, port_number=None):

timestamp()
print(f"{ATF_DB_SUCCESS}Successful connection to database host.")
return atf_db_socket


def host_disconnect():
Expand Down
21 changes: 21 additions & 0 deletions atfdb/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import subprocess
import time as ttime

import pytest

from atfdb.atfdb import host_connect, host_disconnect
Expand All @@ -20,8 +23,26 @@

@pytest.fixture(scope="session")
def socket_server():
p = subprocess.Popen(
"test-socket-server".split(),
start_new_session=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
)
ttime.sleep(2.0)

# Connect to the test socket server with the client
host_connect(SERVER_ADDRESS, SERVER_PORT)

yield

# Disconnect the client from the test socket server
host_disconnect()

# Print logs
# print(f"{logfile[-1] = }")
std_out, std_err = p.communicate()
std_out = std_out.decode()
print(std_out)
p.terminate()
4 changes: 3 additions & 1 deletion atfdb/tests/socket_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ def server_program(seed=0):
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# look closely. The bind() function takes tuple as argument
server_socket.bind((host, port)) # bind host address and port together
print_log(f"Started test socket server at {host}:{port}")

# configure how many client the server can listen simultaneously
server_socket.listen(2)
conn, address = server_socket.accept() # accept new connection
print_log("Connection from: " + str(address))
print_log(f"Connection from: {address}")
data = "greeting"
conn.send(data.encode()) # send data to the client

Expand Down Expand Up @@ -55,6 +56,7 @@ def server_program(seed=0):
conn.send(reply.encode()) # send data to the client
else:
print_log(f"{data = }")
print_log("Closing connection...")
conn.close() # close the connection


Expand Down

0 comments on commit d3cef75

Please sign in to comment.