-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
deactivate-on-placement-fail.go
143 lines (127 loc) · 4.51 KB
/
deactivate-on-placement-fail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implieh.
See the License for the specific language governing permissions and
limitations under the License.
*/
package healthz
import (
"context"
"fmt"
"io"
"log"
"net/http"
"strconv"
"testing"
"time"
chi "github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/dapr/dapr/tests/integration/framework"
"github.com/dapr/dapr/tests/integration/framework/process/daprd"
prochttp "github.com/dapr/dapr/tests/integration/framework/process/http"
"github.com/dapr/dapr/tests/integration/framework/process/placement"
"github.com/dapr/dapr/tests/integration/framework/util"
"github.com/dapr/dapr/tests/integration/suite"
)
func init() {
suite.Register(new(deactivateOnPlacementFail))
}
// deactivateOnPlacementFail ensures that all active actors are deactivated if the connection with Placement fails.
type deactivateOnPlacementFail struct {
daprd *daprd.Daprd
place *placement.Placement
invokedActorsCh chan string
deactivatedActorsCh chan string
}
func (h *deactivateOnPlacementFail) Setup(t *testing.T) []framework.Option {
h.invokedActorsCh = make(chan string, 2)
h.deactivatedActorsCh = make(chan string, 2)
r := chi.NewRouter()
r.Get("/dapr/config", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"entities": ["myactortype"]}`))
})
r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
r.Delete("/actors/{actorType}/{actorId}", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
act := fmt.Sprintf("%s/%s", chi.URLParam(r, "actorType"), chi.URLParam(r, "actorId"))
log.Printf("Received deactivation for actor %s", act)
h.deactivatedActorsCh <- act
})
r.Put("/actors/{actorType}/{actorId}/method/foo", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`bar`))
act := fmt.Sprintf("%s/%s", chi.URLParam(r, "actorType"), chi.URLParam(r, "actorId"))
log.Printf("Received invocation for actor %s", act)
h.invokedActorsCh <- act
})
srv := prochttp.New(t, prochttp.WithHandler(r))
h.place = placement.New(t)
h.daprd = daprd.New(t, daprd.WithResourceFiles(`
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: mystore
spec:
type: state.in-memory
version: v1
metadata:
- name: actorStateStore
value: true
`),
daprd.WithPlacementAddresses("localhost:"+strconv.Itoa(h.place.Port())),
daprd.WithAppProtocol("http"),
daprd.WithAppPort(srv.Port()),
)
return []framework.Option{
framework.WithProcesses(h.place, srv, h.daprd),
}
}
func (h *deactivateOnPlacementFail) Run(t *testing.T, ctx context.Context) {
h.place.WaitUntilRunning(t, ctx)
h.daprd.WaitUntilRunning(t, ctx)
client := util.HTTPClient(t)
// Activate 2 actors
for i := 0; i < 2; i++ {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
daprdURL := fmt.Sprintf("http://localhost:%d/v1.0/actors/myactortype/myactor%d/method/foo", h.daprd.HTTPPort(), i)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, daprdURL, nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equalf(t, http.StatusOK, resp.StatusCode, "Response body: %v", string(body))
}, 10*time.Second, 100*time.Millisecond, "actor not ready")
}
// Validate invocations
invoked := make([]string, 2)
for i := 0; i < 2; i++ {
select {
case invoked[i] = <-h.invokedActorsCh:
case <-time.After(time.Second * 5):
assert.Fail(t, "failed to invoke actor in time")
}
}
assert.ElementsMatch(t, []string{"myactortype/myactor0", "myactortype/myactor1"}, invoked)
// Terminate the placement process to simulate a failure
h.place.Cleanup(t)
// Ensure actors get deactivated
deactivated := make([]string, 2)
for i := range deactivated {
select {
case deactivated[i] = <-h.deactivatedActorsCh:
case <-time.After(5 * time.Second):
assert.Fail(t, "Did not receive deactivation signal in time")
}
}
assert.ElementsMatch(t, []string{"myactortype/myactor0", "myactortype/myactor1"}, deactivated)
}