Skip to content

Commit e2cbce4

Browse files
authored
Implement new_guid API (#265)
1 parent 30b177a commit e2cbce4

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

azure/durable_functions/models/DurableOrchestrationContext.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import datetime
33
from typing import List, Any, Dict, Optional
4+
from uuid import UUID, uuid5, NAMESPACE_URL
45

56
from .RetryOptions import RetryOptions
67
from .TaskSet import TaskSet
@@ -444,3 +445,17 @@ def continue_as_new(self, input_: Any):
444445
The new starting input to the orchestrator.
445446
"""
446447
return continue_as_new(context=self, input_=input_)
448+
449+
def new_guid(self) -> UUID:
450+
"""Generate a replay-safe GUID.
451+
452+
Returns
453+
-------
454+
UUID
455+
A new globally-unique ID
456+
"""
457+
guid_name = f"{self.instance_id}_{self.current_utc_datetime}"\
458+
f"_{self._new_uuid_counter}"
459+
self._new_uuid_counter += 1
460+
guid = uuid5(NAMESPACE_URL, guid_name)
461+
return guid

tests/orchestrator/test_sequential_orchestrator.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ def generator_function_with_serialization(context):
9191

9292
return outputs
9393

94+
def generator_function_new_guid(context):
95+
"""Simple orchestrator that generates 3 GUIDs"""
96+
outputs = []
97+
98+
output1 = context.new_guid()
99+
output2 = context.new_guid()
100+
output3 = context.new_guid()
101+
102+
outputs.append(str(output1))
103+
outputs.append(str(output2))
104+
outputs.append(str(output3))
105+
return outputs
106+
94107

95108
def base_expected_state(output=None) -> OrchestratorState:
96109
return OrchestratorState(is_done=False, actions=[], output=output)
@@ -353,3 +366,22 @@ def test_utc_time_updates_correctly():
353366
assert_valid_schema(result)
354367
assert_orchestration_state_equals(expected, result)
355368

369+
def test_new_guid_orchestrator():
370+
"""Tests that the new_guid API is replay-safe and produces new GUIDs every time"""
371+
context_builder = ContextBuilder('test_guid_orchestrator')
372+
373+
# To test that the API is replay-safe, we generate two orchestrators
374+
# with the same starting context
375+
result1 = get_orchestration_state_result(
376+
context_builder, generator_function_new_guid)
377+
outputs1 = result1["output"]
378+
379+
result2 = get_orchestration_state_result(
380+
context_builder, generator_function_new_guid)
381+
outputs2 = result2["output"]
382+
383+
# All GUIDs should be unique
384+
assert len(outputs1) == len(set(outputs1))
385+
# The two GUID lists should be the same
386+
assert outputs1 == outputs2
387+

0 commit comments

Comments
 (0)