MCPcopy Create free account
hub / github.com/MemTensor/MemOS / test_context_thread_isolation

Function test_context_thread_isolation

tests/api/test_thread_context.py:101–146  ·  view source on GitHub ↗

Test context isolation between different threads

()

Source from the content-addressed store, hash-verified

99
100
101def test_context_thread_isolation():
102 """Test context isolation between different threads"""
103 # Set up main thread context
104 main_context = RequestContext(trace_id="isolation-test-trace")
105 main_context.test_data = "main thread data"
106 set_request_context(main_context)
107
108 results = []
109
110 def thread_task(task_id: str, custom_data: str):
111 # Get and maintain reference to context in child thread
112 context = get_current_context()
113 if context:
114 # Modify context data
115 context.test_data = custom_data
116 # Re-set context to make modifications take effect
117 set_request_context(context)
118
119 # Get modified context data
120 current_context = get_current_context()
121 results.append(
122 {
123 "task_id": task_id,
124 "test_data": current_context.test_data if current_context else None,
125 }
126 )
127
128 # Create two threads with different data
129 thread1 = ContextThread(target=thread_task, args=("thread1", "thread1 data"))
130 thread2 = ContextThread(target=thread_task, args=("thread2", "thread2 data"))
131
132 thread1.start()
133 thread2.start()
134 thread1.join()
135 thread2.join()
136
137 # Verify thread isolation
138 thread1_result = next(r for r in results if r["task_id"] == "thread1")
139 thread2_result = next(r for r in results if r["task_id"] == "thread2")
140
141 assert thread1_result["test_data"] == "thread1 data"
142 assert thread2_result["test_data"] == "thread2 data"
143
144 # Verify main thread context wasn't modified by child threads
145 main_context_after = get_current_context()
146 assert main_context_after.test_data == "main thread data"
147
148
149def test_context_thread_error_with_context():

Callers

nothing calls this directly

Calls 7

RequestContextClass · 0.90
set_request_contextFunction · 0.90
ContextThreadClass · 0.90
get_current_contextFunction · 0.90
nextFunction · 0.85
startMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected