Completed for a grade of three

This commit is contained in:
2026-10-23 14:11:11 +03:00
parent a3dbcd6ba4
commit 92c2fdc254
54 changed files with 1221 additions and 92 deletions
+24
View File
@@ -0,0 +1,24 @@
# before
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 236cb759e825 running Up About a minute (healthy)
mtusi-messenger-lab3-api-2 787a253aa6a6 running Up 8 seconds (healthy)
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 119, "failed_attempts": 1, "first_recovery_seconds": 6.508}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
# after: the stopped container is the replica whose id the test passed as TARGET_INSTANCE_ID
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 236cb759e825 exited Exited (143) 8 seconds ago
mtusi-messenger-lab3-api-2 787a253aa6a6 running Up 24 seconds (healthy)
# HAProxy log around the failure (non-2xx and state changes)
[WARNING] (8) : Server api/api1 was DOWN and now enters maintenance (No IP for server ).
Server api/api1 ('api') is UP/READY (resolves again).
[WARNING] (8) : Server api/api1 ('api') is UP/READY (resolves again).
172.24.0.1:55062 [22/Oct/2026:17:44:22.431] http api/api2 0/0/0/-1/93 502 209 - - SH-- 1/1/0/0/0 0/0 "POST /api/v1/chats/43312e52-5c0b-41af-9981-2379d372b382/messages HTTP/1.1"
[WARNING] (8) : Server api/api2 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Server api/api2 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
Server api/api2 was DOWN and now enters maintenance (No IP for server ).
[WARNING] (8) : Server api/api2 was DOWN and now enters maintenance (No IP for server ).
+24
View File
@@ -0,0 +1,24 @@
# before
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 236cb759e825 running Up 8 seconds (healthy)
mtusi-messenger-lab3-api-2 787a253aa6a6 running Up About a minute (healthy)
$ make test-failover ACTION_SCRIPT=scripts/stop-one.sh
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 120, "failed_attempts": 1, "first_recovery_seconds": 6.537}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
# after: the stopped container is the replica whose id the test passed as TARGET_INSTANCE_ID
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 236cb759e825 running Up 24 seconds (healthy)
mtusi-messenger-lab3-api-2 787a253aa6a6 exited Exited (143) 8 seconds ago
# HAProxy log around the failure (non-2xx and state changes)
[WARNING] (8) : Server api/api2 was DOWN and now enters maintenance (No IP for server ).
Server api/api2 ('api') is UP/READY (resolves again).
[WARNING] (8) : Server api/api2 ('api') is UP/READY (resolves again).
172.24.0.1:62054 [22/Oct/2026:17:45:16.133] http api/api1 0/0/0/-1/87 502 209 - - SH-- 1/1/0/0/0 0/0 "POST /api/v1/chats/43529cee-e036-4e88-a744-c3fd0e23ce65/messages HTTP/1.1"
Server api/api1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[WARNING] (8) : Server api/api1 is DOWN, reason: Layer4 connection problem, info: "Connection refused", check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[WARNING] (8) : Server api/api1 was DOWN and now enters maintenance (No IP for server ).
Server api/api1 was DOWN and now enters maintenance (No IP for server ).
+84
View File
@@ -0,0 +1,84 @@
# ./gradlew test (Testcontainers: postgres:17.6-alpine3.22, redis:8.4.0-alpine)
total: 66 tests, 0 failed
infrastructure.controllers.authentication.AuthenticationControllerTest: 9 tests, 0 failed, 7.774 s
- logout revokes only the current token
- Bearer token cannot open a session
- password containing a colon and non-ASCII characters works
- unknown token returns 401
- session expires after SESSION_TTL_SECONDS
- each login issues a different token
- valid Basic credentials open a session
- missing or malformed Basic header returns 401
- wrong password and unknown user get the same 401
infrastructure.controllers.chat.ChatControllerTest: 6 tests, 0 failed, 2.101 s
- member can read own chat and members keep their order
- unknown member is rejected before anything is written
- foreign chat returns 404 for non-member
- cursor issued to another user is rejected
- chat list contains only own chats and is paginated
- invalid member lists are rejected with 400
infrastructure.controllers.health.HealthControllerTest: 2 tests, 0 failed, 0.134 s
- live returns 200
- ready returns 200 when database is reachable
infrastructure.controllers.message.MessageConcurrencyTest: 1 tests, 0 failed, 0.335 s
- parallel senders produce exactly N unique messages
infrastructure.controllers.message.MessageControllerTest: 7 tests, 0 failed, 1.946 s
- foreign chat returns 404 when sending
- last page has null next_cursor
- pagination walks all messages exactly once
- foreign chat returns 404 when listing
- empty text returns 400 invalid_request
- history that is an exact multiple of limit ends with null next_cursor
- foreign cursor returns 400
infrastructure.controllers.message.MessageIdempotencyTest: 7 tests, 0 failed, 3.266 s
- expired key can be used for a new message
- key scope is user and chat
- same key with another body is a conflict
- missing or malformed key is rejected before anything is written
- retry with the same key and body returns the original message
- retry by a former outsider is still checked for access
- concurrent retries create exactly one message
infrastructure.controllers.user.UserControllerTest: 11 tests, 0 failed, 1.638 s
- password shorter than 12 code points returns 400
- concurrent registrations of one username create exactly one user
- X-User-Id alone gives no access
- create user returns 201 without password
- duplicate username returns 409 conflict
- missing token returns 401 with Bearer challenge
- invalid JSON body returns 400 invalid_request
- missing password returns 400
- Basic credentials are rejected on business endpoints
- me returns the session owner
- X-User-Id does not override bearer identity
infrastructure.repositories.authentication.session.cache.RedisSessionCacheRepositoryTest: 3 tests, 0 failed, 0.102 s
- cached session round-trips and expires together with the session
- revoke replaces a cached session with a tombstone
- late cache fill cannot overwrite a tombstone
infrastructure.repositories.chat.ExposedChatRepositoryTest: 2 tests, 0 failed, 0.514 s
- chat row is rolled back when inserting members fails
- successful creation commits the chat and every member
infrastructure.services.authentication.SessionLookupSingleFlightTest: 4 tests, 0 failed, 1.120 s
- request arriving after logout does not join a lookup started before it
- without single-flight every concurrent miss queries the database
- with single-flight concurrent misses share one database lookup
- lookup that finished before a request arrived is not reused
infrastructure.services.hashing.DefaultHashingServiceTest: 4 tests, 0 failed, 0.421 s
- passwords differing only after 72 bytes are distinguished
- validates the right password and rejects a wrong one
- malformed hash is rejected instead of throwing
- hash is a salted Argon2id PHC string
plugins.DatabaseMigrationTest: 1 tests, 0 failed, 0.209 s
- re-running migrations applies nothing and keeps data
plugins.DependencyFailureTest: 5 tests, 0 failed, 13.326 s
- without PostgreSQL requests fail with a bounded 503
- logout during a Redis outage survives an API restart
- logout during a Redis outage is not undone by a stale cached copy
- without Redis requests bypass the cache
- hanging Redis costs one command timeout and recovers
plugins.InstanceIdTest: 1 tests, 0 failed, 0.231 s
- every response names the replica
plugins.SessionCacheTest: 3 tests, 0 failed, 0.928 s
- emptied cache falls back to PostgreSQL
- first authenticated request fills the cache
- logout leaves a tombstone and the token is rejected
+19
View File
@@ -0,0 +1,19 @@
$ python3 scripts/idempotency-race.py
{
"rounds": 20,
"parallel_requests_per_round": 32,
"statuses": {
"201": 640
},
"requests_per_instance": {
"236cb759e825": 320,
"787a253aa6a6": 320
},
"rounds_with_more_than_one_message_id": 0,
"messages_in_history": 20,
"distinct_texts_in_history": 20
}
PASS
$ psql: keys without a message (must be 0)
0
+21
View File
@@ -0,0 +1,21 @@
$ docker compose ps -a
NAME IMAGE STATE STATUS PORTS
mtusi-messenger-lab3-api-1 mtusi-messenger:lab3 running Up 35 seconds (healthy) 8080/tcp
mtusi-messenger-lab3-api-2 mtusi-messenger:lab3 running Up 35 seconds (healthy) 8080/tcp
mtusi-messenger-lab3-lb-1 haproxy:3.2.4-alpine running Up 29 seconds 127.0.0.1:8080->8080/tcp
mtusi-messenger-lab3-migrate-1 mtusi-messenger:lab3 exited Exited (0) 36 seconds ago
mtusi-messenger-lab3-postgres-1 postgres:17.6-alpine3.22 running Up 42 seconds (healthy) 5432/tcp
mtusi-messenger-lab3-redis-1 redis:8.4.0-alpine running Up 42 seconds (healthy) 6379/tcp
$ for i in 1..6: curl -sD - http://localhost:8080/health/ready | grep X-Instance-Id
x-instance-id: 236cb759e825
x-instance-id: 787a253aa6a6
x-instance-id: 236cb759e825
x-instance-id: 787a253aa6a6
x-instance-id: 236cb759e825
x-instance-id: 787a253aa6a6
# X-Instance-Id = HOSTNAME = short container id of the replica
NAME CONTAINER ID STATE STATUS
mtusi-messenger-lab3-api-1 236cb759e825 running Up 35 seconds (healthy)
mtusi-messenger-lab3-api-2 787a253aa6a6 running Up 35 seconds (healthy)
+24
View File
@@ -0,0 +1,24 @@
# before
NAME STATE STATUS
mtusi-messenger-lab3-api-1 running Up 35 seconds (healthy)
mtusi-messenger-lab3-api-2 running Up 8 seconds (healthy)
mtusi-messenger-lab3-lb-1 running Up 2 minutes
mtusi-messenger-lab3-migrate-1 exited Exited (0) 7 seconds ago
mtusi-messenger-lab3-postgres-1 running Up 2 minutes (healthy)
mtusi-messenger-lab3-redis-1 running Up 2 minutes (healthy)
/mtusi-messenger-lab3-api-1 StartedAt=2026-10-22T17:45:06.396826175Z
/mtusi-messenger-lab3-api-2 StartedAt=2026-10-22T17:45:33.184696798Z
/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-22T17:43:17.31840918Z
/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-22T17:43:04.928814758Z
/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-22T17:43:04.927049216Z
$ make test-persistence ACTION_SCRIPT=scripts/restart.sh
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
# after: every container has a new StartedAt, volume postgres-data is kept
/mtusi-messenger-lab3-api-1 StartedAt=2026-10-22T17:45:53.47890296Z
/mtusi-messenger-lab3-api-2 StartedAt=2026-10-22T17:45:53.585460419Z
/mtusi-messenger-lab3-lb-1 StartedAt=2026-10-22T17:45:59.18815863Z
/mtusi-messenger-lab3-postgres-1 StartedAt=2026-10-22T17:45:46.762570055Z
/mtusi-messenger-lab3-redis-1 StartedAt=2026-10-22T17:45:46.760974013Z
mtusi-messenger-lab3_postgres-data
+2
View File
@@ -0,0 +1,2 @@
$ make test
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.