Skip to content

Commit 9fdc98d

Browse files
committed
add and mature agentsocket tests
1 parent 232ed9f commit 9fdc98d

File tree

4 files changed

+277
-105
lines changed

4 files changed

+277
-105
lines changed

agent/agentsocket/proto/agentsocket.pb.go

Lines changed: 79 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

agent/agentsocket/proto/agentsocket.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ message SyncReadyRequest {
4747
message SyncReadyResponse {
4848
bool success = 1;
4949
string message = 2;
50+
bool is_ready = 3;
5051
}
5152

5253
message SyncStatusRequest {

agent/agentsocket/service.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"time"
77

8+
"golang.org/x/xerrors"
89
"google.golang.org/protobuf/types/known/timestamppb"
910

1011
"cdr.dev/slog"
@@ -14,6 +15,11 @@ import (
1415

1516
var _ proto.DRPCAgentSocketServer = (*DRPCAgentSocketService)(nil)
1617

18+
var (
19+
ErrUnitManagerNotAvailable = xerrors.New("unit manager not available")
20+
ErrUnitNameRequired = xerrors.New("unit name is required")
21+
)
22+
1723
type DRPCAgentSocketService struct {
1824
unitManager *unit.Manager[string]
1925
logger slog.Logger
@@ -30,14 +36,14 @@ func (s *DRPCAgentSocketService) SyncStart(_ context.Context, req *proto.SyncSta
3036
if s.unitManager == nil {
3137
return &proto.SyncStartResponse{
3238
Success: false,
33-
Message: "dependency tracker not available",
39+
Message: ErrUnitManagerNotAvailable.Error(),
3440
}, nil
3541
}
3642

3743
if req.Unit == "" {
3844
return &proto.SyncStartResponse{
3945
Success: false,
40-
Message: "Unit name is required",
46+
Message: ErrUnitNameRequired.Error(),
4147
}, nil
4248
}
4349

@@ -46,7 +52,7 @@ func (s *DRPCAgentSocketService) SyncStart(_ context.Context, req *proto.SyncSta
4652
if !errors.Is(err, unit.ErrUnitAlreadyRegistered) {
4753
return &proto.SyncStartResponse{
4854
Success: false,
49-
Message: "Failed to register unit: " + err.Error(),
55+
Message: xerrors.Errorf("failed to register unit: %w", err).Error(),
5056
}, nil
5157
}
5258
}
@@ -55,13 +61,13 @@ func (s *DRPCAgentSocketService) SyncStart(_ context.Context, req *proto.SyncSta
5561
if err != nil {
5662
return &proto.SyncStartResponse{
5763
Success: false,
58-
Message: "Failed to check readiness: " + err.Error(),
64+
Message: xerrors.Errorf("failed to check readiness: %w", err).Error(),
5965
}, nil
6066
}
6167
if !isReady {
6268
return &proto.SyncStartResponse{
6369
Success: false,
64-
Message: "Unit is not ready",
70+
Message: xerrors.Errorf("unit is not ready: %w", unit.ErrDependenciesNotSatisfied).Error(),
6571
}, nil
6672
}
6773

@@ -82,14 +88,14 @@ func (s *DRPCAgentSocketService) SyncWant(_ context.Context, req *proto.SyncWant
8288
if s.unitManager == nil {
8389
return &proto.SyncWantResponse{
8490
Success: false,
85-
Message: "unit manager not available",
91+
Message: ErrUnitManagerNotAvailable.Error(),
8692
}, nil
8793
}
8894

8995
if req.Unit == "" || req.DependsOn == "" {
9096
return &proto.SyncWantResponse{
9197
Success: false,
92-
Message: "unit and depends_on are required",
98+
Message: ErrUnitNameRequired.Error(),
9399
}, nil
94100
}
95101

@@ -128,14 +134,14 @@ func (s *DRPCAgentSocketService) SyncComplete(_ context.Context, req *proto.Sync
128134
if s.unitManager == nil {
129135
return &proto.SyncCompleteResponse{
130136
Success: false,
131-
Message: "unit manager not available",
137+
Message: ErrUnitManagerNotAvailable.Error(),
132138
}, nil
133139
}
134140

135141
if req.Unit == "" {
136142
return &proto.SyncCompleteResponse{
137143
Success: false,
138-
Message: "unit name is required",
144+
Message: ErrUnitNameRequired.Error(),
139145
}, nil
140146
}
141147

@@ -156,14 +162,14 @@ func (s *DRPCAgentSocketService) SyncReady(_ context.Context, req *proto.SyncRea
156162
if s.unitManager == nil {
157163
return &proto.SyncReadyResponse{
158164
Success: false,
159-
Message: "unit manager not available",
165+
Message: ErrUnitManagerNotAvailable.Error(),
160166
}, nil
161167
}
162168

163169
if req.Unit == "" {
164170
return &proto.SyncReadyResponse{
165171
Success: false,
166-
Message: "unit name is required",
172+
Message: ErrUnitNameRequired.Error(),
167173
}, nil
168174
}
169175

@@ -172,34 +178,28 @@ func (s *DRPCAgentSocketService) SyncReady(_ context.Context, req *proto.SyncRea
172178
return &proto.SyncReadyResponse{
173179
Success: false,
174180
Message: "failed to check readiness: " + err.Error(),
175-
}, nil
176-
}
177-
178-
if !isReady {
179-
return &proto.SyncReadyResponse{
180-
Success: false,
181-
Message: unit.ErrDependenciesNotSatisfied.Error(),
181+
IsReady: false,
182182
}, nil
183183
}
184184

185185
return &proto.SyncReadyResponse{
186186
Success: true,
187-
Message: "unit " + req.Unit + " dependencies are satisfied",
187+
IsReady: isReady,
188188
}, nil
189189
}
190190

191191
func (s *DRPCAgentSocketService) SyncStatus(_ context.Context, req *proto.SyncStatusRequest) (*proto.SyncStatusResponse, error) {
192192
if s.unitManager == nil {
193193
return &proto.SyncStatusResponse{
194194
Success: false,
195-
Message: "unit manager not available",
195+
Message: ErrUnitManagerNotAvailable.Error(),
196196
}, nil
197197
}
198198

199199
if req.Unit == "" {
200200
return &proto.SyncStatusResponse{
201201
Success: false,
202-
Message: "unit name is required",
202+
Message: ErrUnitNameRequired.Error(),
203203
}, nil
204204
}
205205

0 commit comments

Comments
 (0)