17
17
use Temporal \Worker \WorkerOptions ;
18
18
use Temporal \Workflow \WorkflowInterface ;
19
19
use Temporal \Workflow \WorkflowMethod ;
20
+ use Temporal \Workflow \WorkflowVersioningBehavior ;
20
21
21
22
#[Worker(options: [WorkerFactory::class, 'options ' ])]
22
23
class DeploymentTest extends TestCase
23
24
{
24
25
#[Test]
25
- public function sendEmpty (
26
+ public function defaultBehaviorAuto (
26
27
TemporalStarter $ starter ,
27
28
WorkflowClientInterface $ client ,
28
29
Feature $ feature ,
29
30
): void {
30
- $ starter ->executeTemporalCommand ([
31
- 'worker ' ,
32
- 'deployment ' ,
33
- 'set-current-version ' ,
34
- '--deployment-name ' , WorkerFactory::DEPLOYMENT_NAME ,
35
- '--build-id ' , WorkerFactory::BUILD_ID ,
36
- '--yes ' ,
37
- ], timeout: 5 );
31
+ WorkerFactory::setCurrentDeployment ($ starter );
38
32
39
33
try {
40
34
# Create a Workflow stub with an execution timeout 12 seconds
41
35
$ stub = $ client
42
36
->withTimeout (10 )
43
37
->newUntypedWorkflowStub (
44
- 'Extra_Versioning_Deployment ' ,
38
+ /** @see PinnedWorkflow */
39
+ 'Extra_Versioning_Deployment_Default ' ,
45
40
WorkflowOptions::new ()
46
41
->withTaskQueue ($ feature ->taskQueue )
47
42
->withWorkflowExecutionTimeout (20 ),
@@ -50,9 +45,11 @@ public function sendEmpty(
50
45
# Start the Workflow
51
46
$ client ->start ($ stub );
52
47
48
+ # Check the result
53
49
$ result = $ stub ->getResult (timeout: 5 );
54
50
self ::assertSame ('default ' , $ result );
55
51
52
+ # Check the Workflow History
56
53
$ version = null ;
57
54
$ behavior = null ;
58
55
foreach ($ client ->getWorkflowHistory ($ stub ->getExecution ()) as $ event ) {
@@ -71,6 +68,53 @@ public function sendEmpty(
71
68
$ starter ->stop () and $ starter ->start ();
72
69
}
73
70
}
71
+
72
+ #[Test]
73
+ public function customBehaviorPinned (
74
+ TemporalStarter $ starter ,
75
+ WorkflowClientInterface $ client ,
76
+ Feature $ feature ,
77
+ ): void {
78
+ WorkerFactory::setCurrentDeployment ($ starter );
79
+
80
+ try {
81
+ # Create a Workflow stub with an execution timeout 12 seconds
82
+ $ stub = $ client
83
+ ->withTimeout (10 )
84
+ ->newUntypedWorkflowStub (
85
+ /** @see DefaultWorkflow */
86
+ 'Extra_Versioning_Deployment_Pinned ' ,
87
+ WorkflowOptions::new ()
88
+ ->withTaskQueue ($ feature ->taskQueue )
89
+ ->withWorkflowExecutionTimeout (20 ),
90
+ );
91
+
92
+ # Start the Workflow
93
+ $ client ->start ($ stub );
94
+
95
+ # Check the result
96
+ $ result = $ stub ->getResult (timeout: 5 );
97
+ self ::assertSame ('pinned ' , $ result );
98
+
99
+ # Check the Workflow History
100
+ $ version = null ;
101
+ $ behavior = null ;
102
+ foreach ($ client ->getWorkflowHistory ($ stub ->getExecution ()) as $ event ) {
103
+ if ($ event ->hasWorkflowTaskCompletedEventAttributes ()) {
104
+ $ version = $ event ->getWorkflowTaskCompletedEventAttributes ()?->getDeploymentVersion();
105
+ $ behavior = $ event ->getWorkflowTaskCompletedEventAttributes ()?->getVersioningBehavior();
106
+ break ;
107
+ }
108
+ }
109
+
110
+ self ::assertNotNull ($ version );
111
+ self ::assertSame (WorkerFactory::DEPLOYMENT_NAME , $ version ->getDeploymentName ());
112
+ self ::assertSame (WorkerFactory::BUILD_ID , $ version ->getBuildId ());
113
+ self ::assertSame (VersioningBehavior::Pinned->value , $ behavior );
114
+ } finally {
115
+ $ starter ->stop () and $ starter ->start ();
116
+ }
117
+ }
74
118
}
75
119
76
120
class WorkerFactory
@@ -88,14 +132,37 @@ public static function options(): WorkerOptions
88
132
->withDefaultVersioningBehavior (VersioningBehavior::AutoUpgrade),
89
133
);
90
134
}
135
+
136
+ public static function setCurrentDeployment (TemporalStarter $ starter ): void
137
+ {
138
+ $ starter ->executeTemporalCommand ([
139
+ 'worker ' ,
140
+ 'deployment ' ,
141
+ 'set-current-version ' ,
142
+ '--deployment-name ' , WorkerFactory::DEPLOYMENT_NAME ,
143
+ '--build-id ' , WorkerFactory::BUILD_ID ,
144
+ '--yes ' ,
145
+ ], timeout: 5 );
146
+ }
91
147
}
92
148
93
149
#[WorkflowInterface]
94
- class TestWorkflow
150
+ class DefaultWorkflow
95
151
{
96
- #[WorkflowMethod(name: "Extra_Versioning_Deployment " )]
152
+ #[WorkflowMethod(name: "Extra_Versioning_Deployment_Default " )]
97
153
public function handle ()
98
154
{
99
155
return 'default ' ;
100
156
}
101
157
}
158
+
159
+ #[WorkflowInterface]
160
+ class PinnedWorkflow
161
+ {
162
+ #[WorkflowMethod(name: "Extra_Versioning_Deployment_Pinned " )]
163
+ #[WorkflowVersioningBehavior(VersioningBehavior::Pinned)]
164
+ public function handle ()
165
+ {
166
+ return 'pinned ' ;
167
+ }
168
+ }
0 commit comments