Skip to content

Commit fa6def3

Browse files
authored
increase the history record limit to 1000 (#19)
add a flag to set the limit as well Co-authored-by: rick <[email protected]>
1 parent 4fb0d00 commit fa6def3

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

cmd/root.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func NewRootCommand() (c *cobra.Command) {
3232
RunE: opt.runE,
3333
}
3434
opt.AddFlags(c.Flags())
35+
c.Flags().IntVarP(&opt.historyLimit, "history-limit", "", 1000, "History record items count limit")
3536
c.Flags().BoolVarP(&opt.version, "version", "", false, "Print the version then exit")
3637
return
3738
}
@@ -42,12 +43,13 @@ func (o *option) runE(c *cobra.Command, args []string) (err error) {
4243
c.Println(version.GetDate())
4344
return
4445
}
45-
remoteServer := pkg.NewRemoteServer()
46+
remoteServer := pkg.NewRemoteServer(o.historyLimit)
4647
err = ext.CreateRunner(o.Extension, c, remoteServer)
4748
return
4849
}
4950

5051
type option struct {
5152
*ext.Extension
52-
version bool
53+
historyLimit int
54+
version bool
5355
}

pkg/server.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ import (
1919
"context"
2020
"errors"
2121
"fmt"
22+
"log"
23+
"os"
24+
"path/filepath"
25+
"strconv"
26+
"strings"
27+
2228
"github.com/linuxsuren/api-testing/pkg/extension"
2329
"github.com/linuxsuren/api-testing/pkg/server"
2430
"github.com/linuxsuren/api-testing/pkg/testing/remote"
@@ -29,20 +35,18 @@ import (
2935
"gorm.io/driver/sqlite"
3036
"gorm.io/gorm"
3137
"gorm.io/gorm/logger"
32-
"log"
33-
"os"
34-
"path/filepath"
35-
"strconv"
36-
"strings"
3738
)
3839

3940
type dbserver struct {
4041
remote.UnimplementedLoaderServer
42+
defaultHistoryLimit int
4143
}
4244

4345
// NewRemoteServer creates a remote server instance
44-
func NewRemoteServer() (s remote.LoaderServer) {
45-
s = &dbserver{}
46+
func NewRemoteServer(defaultHistoryLimit int) (s remote.LoaderServer) {
47+
s = &dbserver{
48+
defaultHistoryLimit: defaultHistoryLimit,
49+
}
4650
return
4751
}
4852

@@ -242,10 +246,12 @@ func (s *dbserver) CreateTestCaseHistory(ctx context.Context, historyTestResult
242246
}
243247

244248
store := remote.GetStoreFromContext(ctx)
245-
historyLimit := 10
249+
historyLimit := s.defaultHistoryLimit
246250
if v, ok := store.Properties["historyLimit"]; ok {
247251
if parsedHistoryLimit, parseErr := strconv.Atoi(v); parseErr == nil {
248252
historyLimit = parsedHistoryLimit
253+
} else {
254+
log.Printf("failed to parse history limit: %v\n", parseErr)
249255
}
250256
}
251257

@@ -255,15 +261,15 @@ func (s *dbserver) CreateTestCaseHistory(ctx context.Context, historyTestResult
255261
if count >= int64(historyLimit) {
256262
var oldestRecord HistoryTestResult
257263
if err = db.Order("create_time").First(&oldestRecord).Error; err != nil {
258-
fmt.Printf("Error find oldest record: %v\n", err)
264+
log.Printf("Error find oldest record: %v\n", err)
259265
return
260266
}
261267

262268
if err = db.Delete(&oldestRecord).Error; err != nil {
263-
fmt.Printf("Error delete oldest record: %v\n", err)
269+
log.Printf("Error delete oldest record: %v\n", err)
264270
return
265271
}
266-
fmt.Printf("Existing count: %d, limit: %d\nmaximum number of entries reached.\n", count, historyLimit)
272+
log.Printf("Existing count: %d, limit: %d\nmaximum number of entries reached.\n", count, historyLimit)
267273
}
268274

269275
db.Create(ConvertToDBHistoryTestResult(historyTestResult))

pkg/server_test.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ package pkg
1818
import (
1919
"context"
2020
"fmt"
21+
"os"
22+
"testing"
23+
"time"
24+
2125
"github.com/linuxsuren/api-testing/pkg/server"
2226
atest "github.com/linuxsuren/api-testing/pkg/testing"
2327
"github.com/linuxsuren/api-testing/pkg/testing/remote"
2428
"github.com/stretchr/testify/assert"
2529
"google.golang.org/protobuf/types/known/timestamppb"
26-
"os"
27-
"testing"
28-
"time"
2930
)
3031

3132
func TestNewRemoteServer(t *testing.T) {
32-
remoteServer := NewRemoteServer()
33+
remoteServer := NewRemoteServer(10)
3334
assert.NotNil(t, remoteServer)
3435
defaultCtx := context.Background()
3536

@@ -120,7 +121,7 @@ func TestNewRemoteServer(t *testing.T) {
120121
})
121122

122123
t.Run("invalid orm driver", func(t *testing.T) {
123-
remoteServer := NewRemoteServer()
124+
remoteServer := NewRemoteServer(10)
124125
assert.NotNil(t, remoteServer)
125126
defaultCtx := remote.WithIncomingStoreContext(context.TODO(), &atest.Store{
126127
Properties: map[string]string{
@@ -132,7 +133,7 @@ func TestNewRemoteServer(t *testing.T) {
132133
})
133134

134135
t.Run("invalid mysql config", func(t *testing.T) {
135-
remoteServer := NewRemoteServer()
136+
remoteServer := NewRemoteServer(10)
136137
assert.NotNil(t, remoteServer)
137138
defaultCtx := remote.WithIncomingStoreContext(context.TODO(), &atest.Store{
138139
Properties: map[string]string{
@@ -144,7 +145,7 @@ func TestNewRemoteServer(t *testing.T) {
144145
})
145146

146147
t.Run("invalid postgres config", func(t *testing.T) {
147-
remoteServer := NewRemoteServer()
148+
remoteServer := NewRemoteServer(10)
148149
assert.NotNil(t, remoteServer)
149150
defaultCtx := remote.WithIncomingStoreContext(context.TODO(), &atest.Store{
150151
Properties: map[string]string{
@@ -158,7 +159,7 @@ func TestNewRemoteServer(t *testing.T) {
158159
}
159160

160161
func TestSQLite(t *testing.T) {
161-
remoteServer := NewRemoteServer()
162+
remoteServer := NewRemoteServer(10)
162163
assert.NotNil(t, remoteServer)
163164
defaultCtx := remote.WithIncomingStoreContext(context.TODO(), &atest.Store{
164165
Properties: map[string]string{

0 commit comments

Comments
 (0)