Skip to content

Commit

Permalink
fix: test case
Browse files Browse the repository at this point in the history
  • Loading branch information
renlulu committed Sep 7, 2024
1 parent bf9b5dc commit 2ae64de
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions das/google_cloud_storage_service_test.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,80 @@
package das

import (
"bytes"
googlestorage "cloud.google.com/go/storage"
"context"
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/offchainlabs/nitro/cmd/genericconf"
"github.com/offchainlabs/nitro/das/dastree"
"testing"
"time"
)

type mockGCSClient struct {
storage map[string][]byte
}

func (c *mockGCSClient) Bucket(name string) *googlestorage.BucketHandle {
//TODO implement me
panic("implement me")
return nil
}

func (c *mockGCSClient) Download(ctx context.Context, bucket, objectPrefix string, key common.Hash) ([]byte, error) {
return nil, ErrNotFound
value, ok := c.storage[objectPrefix+EncodeStorageServiceKey(key)]
if !ok {
return nil, ErrNotFound
}
return value, nil
}

func (c *mockGCSClient) Close(ctx context.Context) error {
//TODO implement me
panic("implement me")
return nil
}

func (*mockGCSClient) Upload(ctx context.Context, bucket, objectPrefix string, value []byte) error {
func (c *mockGCSClient) Upload(ctx context.Context, bucket, objectPrefix string, value []byte) error {
key := objectPrefix + EncodeStorageServiceKey(dastree.Hash(value))
c.storage[key] = value
return nil
}

func NewTestGoogleCloudStorageService(ctx context.Context, googleCloudStorageConfig genericconf.GoogleCloudStorageConfig) (StorageService, error) {
return &GoogleCloudStorageService{
bucket: googleCloudStorageConfig.Bucket,
objectPrefix: googleCloudStorageConfig.ObjectPrefix,
operator: &mockGCSClient{},
operator: &mockGCSClient{
storage: make(map[string][]byte),
},
}, nil
}

func TestNewGoogleCloudStorageService(t *testing.T) {
ctx := context.Background()
//timeout := uint64(time.Now().Add(time.Hour).Unix())
timeout := uint64(time.Now().Add(time.Hour).Unix())
googleCloudService, err := NewTestGoogleCloudStorageService(ctx, genericconf.DefaultGoogleCloudStorageConfig)
Require(t, err)

val1 := []byte("The first value")
val1CorrectKey := dastree.Hash(val1)
//val2IncorrectKey := dastree.Hash(append(val1, 0))
val2IncorrectKey := dastree.Hash(append(val1, 0))

_, err = googleCloudService.GetByHash(ctx, val1CorrectKey)
if !errors.Is(err, ErrNotFound) {
t.Fatal(err)
}

err = googleCloudService.Put(ctx, val1, timeout)
Require(t, err)

_, err = googleCloudService.GetByHash(ctx, val2IncorrectKey)
if !errors.Is(err, ErrNotFound) {
t.Fatal(err)
}

val, err := googleCloudService.GetByHash(ctx, val1CorrectKey)
Require(t, err)
if !bytes.Equal(val, val1) {
t.Fatal(val, val1)
}

}

0 comments on commit 2ae64de

Please sign in to comment.