agentleFS
MicrosoftKnown publishermicrosoft/bocpy/.github/skills/testing-message-queue/SKILL.md

testing-message-queue

Skill198 starsChanged 6 months agoMITView on GitHub

Write tests for the bocpy message queue — the lock-free tag-based MPSC ring buffer. Use when: testing send/receive, FIFO ordering, selective receive, timeouts, cross-thread messaging, multi-producer scenarios, tag capacity limits, set_tags behavior, or payload round-trip fidelity.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Every test must start with a clean slate.

    line 28
  2. Very specific:

    Use set_tags tests to verify pre-assignment, clearing, overflow, and reset behavior.

    line 213

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Testing the Message Queue

This skill describes how to write tests for the bocpy message queue — the lock-free, tag-based MPSC ring buffer implemented in C that underlies send()/receive().

Architecture Overview

ComponentDetail
Queues16 statically allocated ring buffers (BOC_QUEUE_COUNT = 16), each with capacity 16 384.
TagsString labels that map 1:1 to queues. Each unique tag occupies one queue slot.
Assignment modesAutomatic (default): tags claim a queue on first send/receive. Explicit: set_tags() pre-assigns tags, clears all messages, and resets queues.
set_tags([])Drains every queue, frees all tag associations, and sets all queues to UNASSIGNED — effectively restoring auto-assign mode.
Thread safetyEnqueue is lock-free (atomic CAS on tail). Multiple threads can send to the same tag concurrently. receive spins on the queue with a sleep until a message is available or a timeout elapses.

Test File Location

Message queue tests live in test/test_message_queue.py.

Fixture: Resetting Queue State

Every test must start with a clean slate. Use an autouse fixture that calls set_tags([]) before and after each test. This drains all queues and returns them to auto-assign mode — no set_tags call is needed inside individual tests unless the test is specifically exercising set_tags behavior.

MAX_QUEUES = 16

@pytest.fixture(autouse=True)
def reset_queues():
    set_tags([])
    yield
    set_tags([])

Why set_tags([]) instead of set_tags([...16 tags...])?

Calling set_tags is optional. The system auto-assigns tags to queues on first use. The purpose of the fixture is to clear stale state, not to pre-allocate tags. Using set_tags([]) resets all 16 queues to UNASSIGNED so every test starts fresh, and most tests can exercise the auto-assignment path naturally.

Pattern 1 — Basic Send / Receive

The simplest test: send a message, receive it, assert on the result. No set_tags needed — the tag auto-assigns.

def test_basic():
    send("my_tag", "hello")
    tag, val = receive("my_tag", 1)
    assert tag == "my_tag"
    assert val == "hello"

Always pass a timeout to receive so tests fail fast rather than hanging.

Pattern 2 — Verifying FIFO Order

Messages on the same tag are delivered in FIFO order. Send a sequence, then receive and compare element-by-element.

def test_fifo():
    for i in range(10):
        send("fifo", i)
    for i in range(10):
        _, val = receive("fifo", 1)
        assert val == i

Pattern 3 — Selective Receive Across Multiple Tags

receive accepts a list or tuple of tags and returns the first available match. Messages on non-matching tags remain in their queues.

def test_selective():
    send("skip", "no")
    send("want", "yes")
    _, val = receive("want", 1)
    assert val == "yes"
    # "skip" message is still there
    _, val = receive("skip", 1)
    assert val == "no"

def test_multi_tag_receive():
    send("b", "found")
    tag, val = receive(["a", "b", "c"], 1)
    assert tag == "b"
    assert val == "found"

Pattern 4 — Timeout Behavior

Timeout valueBehavior
0Return immediately — delivers a queued message or (TIMEOUT, None).
Positive floatWait up to that many seconds.
Negative (default)Block indefinitely until a message arrives.

The optional after callback fires only on timeout and replaces the default (TIMEOUT, None) return value.

def test_zero_timeout():
    tag, val = receive("empty", 0)
    assert tag == TIMEOUT
    assert val is None

def test_after_callback():
    tag, val = receive("miss", 0.05, lambda: ("fallback", 99))
    assert tag == "fallback"
    assert val == 99

def test_after_not_called_on_success():
    called = []
    send("ok", "payload")
    tag, val = receive(
        "ok", 1,
        lambda: (called.append(True), ("fb", -1))[-1],
    )
    assert tag == "ok"
    assert called == []

Pattern 5 — Cross-Thread Messaging

send and receive are thread-safe. Use threading.Thread to test cross-thread delivery.

def test_cross_thread():
    def sender():
        send("ct", "from_thread")

    t = threading.Thread(target=sender)
    t.start()
    tag, val = receive("ct", 5)
    t.join()
    assert tag == "ct"
    assert val == "from_thread"

def test_bidirectional():
    def echo():
        _, val = receive("ping", 5)
        send("pong", val * 2)

    t = threading.Thread(target=echo)
    t.start()
    send("ping", 21)
    _, val = receive("pong", 5)
    t.join()
    assert val == 42

Pattern 6 — Multiple Concurrent Producers

Multiple threads sending to the same tag exercises the lock-free MPSC enqueue. Collect all messages and verify completeness. Per-producer FIFO ordering is guaranteed (messages from a single thread arrive in send order).

def test_multi_producer():
    n_producers = 8
    msgs_per = 200

    def producer(pid):
        for i in range(msgs_per):
            send("mp", (pid, i))

    threads = [threading.Thread(target=producer, args=(p,))
               for p in range(n_producers)]
    for t in threads:
        t.start()

    received = []
    for _ in range(n_producers * msgs_per):
        _, val = receive("mp", 10)
        received.append(val)

    for t in threads:
        t.join()

    # Every (pid, seq) pair delivered exactly once
    assert sorted(received) == sorted(
        (p, i) for p in range(n_producers) for i in range(msgs_per)
    )

Pattern 7 — Tag Capacity Limits

There are exactly 16 queue slots. Exceeding them raises KeyError.

def test_capacity_exceeded():
    for i in range(MAX_QUEUES):
        send(f"tag{i}", "fill")
    with pytest.raises(KeyError, match="tag capacity exceeded"):
        send("one_too_many", "boom")

Pattern 8 — Testing set_tags Explicitly

Use set_tags tests to verify pre-assignment, clearing, overflow, and reset behavior. These tests call set_tags directly inside the test body.

def test_set_tags_clears():
    send("old", "stale")
    set_tags(["new"])
    # Old messages are gone; "new" starts empty.
    tag, val = receive("new", 0)
    assert tag == TIMEOUT

def test_set_tags_resets_to_auto():
    set_tags(["pre"])
    send("pre", "msg")
    set_tags([])  # back to auto-assign
    send("fresh", "works")
    tag, val = receive("fresh", 1)
    assert val == "works"

def test_set_tags_overflow():
    set_tags([f"t{i}" for i in range(MAX_QUEUES)])
    with pytest.raises(KeyError, match="tag capacity exceeded"):
        send("extra", "x")

def test_set_tags_rejects_too_many():
    with pytest.raises(IndexError):
        set_tags([f"t{i}" for i in range(MAX_QUEUES + 1)])

Pattern 9 — Payload Round-Trip Fidelity

Messages cross interpreter boundaries via cross-interpreter data (XIData) or pickle. Use @pytest.mark.parametrize to sweep payload types.

@pytest.mark.parametrize("payload", [
    0, -1, 2**31, 3.14, float("inf"),
    True, False, None,
    "", "hello",
    (), (1,), (1, (2, (3,))),
])
def test_roundtrip(payload):
    send("rt", payload)
    _, val = receive("rt", 1)
    assert val == payload

Error Handling Tests

ErrorHow to triggerExpected exception
Non-string tag in sendsend(123, "x")TypeError
Empty tag list in receivereceive([], 0)RuntimeError
Non-string in tag listreceive([123], 0)TypeError
Tag capacity exceededsend on 17th unique tagKeyError
Too many tags in set_tagsset_tags([...17 items...])IndexError
Non-str in set_tagsset_tags([123])TypeError
Non-sequence in set_tagsset_tags(42)TypeError

Common Pitfalls

PitfallFix
Calling receive without a timeoutTests hang forever if no message arrives. Always pass a timeout.
Using too many unique tags across testsEach unique tag consumes a queue slot. The fixture must reset queues between tests via set_tags([]).
Forgetting the fixture resets queuesWithout the autouse fixture, stale messages or tag assignments leak between tests, causing flaky failures.
Assuming set_tags is requiredIt isn't. Tags auto-assign on first use. Only call set_tags when you need to pre-assign or clear queues.
Not joining threads before assertingA thread may still hold the GIL or be mid-send. Always t.join() before final assertions.
Sending from more threads than queue capacityIf threads use distinct tags, you'll hit the 16-queue limit. Have concurrent threads share a single tag.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.