Skip to content
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c8292bd
Fix nil pointer error
SarahFrench Aug 19, 2025
1e6befc
Add WIP test for ReadStateBytes
SarahFrench Aug 19, 2025
405d6e5
Move test mock to separate testing file
SarahFrench Aug 20, 2025
0858af9
Update mock to send unexpected EOF when there's a problem returning d…
SarahFrench Aug 20, 2025
0412f11
Add test case for when length != expected length
SarahFrench Aug 20, 2025
b8000cd
Add test for when trying to read state from a store type that doesn't…
SarahFrench Aug 20, 2025
b27984b
Change symbol names to lowercase
SarahFrench Aug 20, 2025
c95856d
Add ability to force a diagnostic to be returned from `mockReadStateB…
SarahFrench Aug 20, 2025
d30d873
Add test showing error diagnostics raised by the ReadStateBytes clien…
SarahFrench Aug 20, 2025
5ab0a35
Add missing header
SarahFrench Aug 20, 2025
cef5231
Simplify mock by using an embedded type
SarahFrench Aug 20, 2025
c888854
Rename `mockOpts` to `mockReadStateBytesOpts`
SarahFrench Aug 20, 2025
1ad54af
Update existing tests to assert what arguments are passed to the RPC …
SarahFrench Aug 20, 2025
41368be
Add mock WriteStateBytesClient which uses `go.uber.org/mock/gomock` t…
SarahFrench Aug 20, 2025
2139777
Add a test for WriteStateBytes that makes assertions about calls to t…
SarahFrench Aug 20, 2025
7c6f297
Update test case to explicitly test writing data smaller than the chu…
SarahFrench Aug 20, 2025
ee38d36
Implement chunking in WriteStateBytes, add test case to assert expect…
SarahFrench Aug 20, 2025
072e7d1
Add generated mock for Provider_WriteStateBytesClient in protocol v6
SarahFrench Aug 21, 2025
ec782bb
Update tests to use new `MockProvider_WriteStateBytesClient`, remove …
SarahFrench Aug 21, 2025
6aa41da
Update code comments in test
SarahFrench Aug 21, 2025
74fab8c
Add tests for diagnostics and errors returned during WriteStateBytes
SarahFrench Aug 21, 2025
c8fda33
Add generated mock for Provider_ReadStateBytesClient in protocol v6, …
SarahFrench Aug 22, 2025
bd16f0b
Add test case for grpc errors in ReadStateBytes, fix how error is ret…
SarahFrench Aug 22, 2025
c87fd4d
Typo in comment
SarahFrench Aug 26, 2025
c348222
Add missing warning test, rename some test cases
SarahFrench Aug 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 36 additions & 23 deletions internal/plugin6/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@ func (p *GRPCProvider) ReadStateBytes(r providers.ReadStateBytesRequest) (resp p
return resp
}

var buf *bytes.Buffer
buf := &bytes.Buffer{}
var expectedTotalLength int
for {
chunk, err := client.Recv()
Expand All @@ -1519,7 +1519,7 @@ func (p *GRPCProvider) ReadStateBytes(r providers.ReadStateBytesRequest) (resp p
break
}
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(err)
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
break
}
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(chunk.Diagnostics))
Expand Down Expand Up @@ -1576,39 +1576,52 @@ func (p *GRPCProvider) WriteStateBytes(r providers.WriteStateBytesRequest) (resp
// TODO: Configurable chunk size
chunkSize := 4 * 1_000_000 // 4MB

if len(r.Bytes) < chunkSize {
client, err := p.client.WriteStateBytes(ctx)
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
return resp
}

buf := bytes.NewBuffer(r.Bytes)
var totalLength int64 = int64(len(r.Bytes))
var totalBytesProcessed int
for {
chunk := buf.Next(chunkSize)

if len(chunk) == 0 {
// The previous iteration read the last of the data. Now we finish up.
protoResp, err := client.CloseAndRecv()
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
return resp
}
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
if resp.Diagnostics.HasErrors() {
return resp
}
break
}

// There is more data to write
protoReq := &proto6.WriteStateBytes_RequestChunk{
TypeName: r.TypeName,
StateId: r.StateId,
Bytes: r.Bytes,
TotalLength: int64(len(r.Bytes)),
Bytes: chunk,
TotalLength: totalLength,
Range: &proto6.StateRange{
Start: 0,
End: int64(len(r.Bytes)),
Start: int64(totalBytesProcessed),
End: int64(totalBytesProcessed + len(chunk)),
},
}
client, err := p.client.WriteStateBytes(ctx)
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
return resp
}
err = client.Send(protoReq)
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
return resp
}
protoResp, err := client.CloseAndRecv()
if err != nil {
resp.Diagnostics = resp.Diagnostics.Append(grpcErr(err))
return resp
}
resp.Diagnostics = resp.Diagnostics.Append(convert.ProtoToDiagnostics(protoResp.Diagnostics))
if resp.Diagnostics.HasErrors() {
return resp
}
}

// TODO: implement chunking for state files larger than chunkSize
// Track progress before next iteration
totalBytesProcessed += len(chunk)
}

return resp
}
Expand Down
Loading