From 22123e548c519e98224ca60ff8b1160f7eb9ddc1 Mon Sep 17 00:00:00 2001 From: xiaohan <95028555@qq.com> Date: Mon, 19 Jul 2021 09:11:46 +0800 Subject: [PATCH 1/2] fix interface set not return error --- interface_store.go | 2 +- store_memory.go | 3 ++- store_memory_test.go | 16 ++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/interface_store.go b/interface_store.go index 361fd4a..9ed6eac 100644 --- a/interface_store.go +++ b/interface_store.go @@ -9,7 +9,7 @@ package base64Captcha // method after the certain amount of captchas has been stored.) type Store interface { // Set sets the digits for the captcha id. - Set(id string, value string) + Set(id string, value string) error // Get returns stored digits for the captcha id. Clear indicates // whether the captcha must be deleted from the store. diff --git a/store_memory.go b/store_memory.go index d420dcf..aab4c7d 100644 --- a/store_memory.go +++ b/store_memory.go @@ -53,7 +53,7 @@ func NewMemoryStore(collectNum int, expiration time.Duration) Store { return s } -func (s *memoryStore) Set(id string, value string) { +func (s *memoryStore) Set(id string, value string) error { s.Lock() s.digitsById[id] = value s.idByTime.PushBack(idByTimeValue{time.Now(), id}) @@ -62,6 +62,7 @@ func (s *memoryStore) Set(id string, value string) { if s.numStored > s.collectNum { go s.collect() } + return nil } func (s *memoryStore) Verify(id, answer string, clear bool) bool { diff --git a/store_memory_test.go b/store_memory_test.go index e41cd0f..df68764 100644 --- a/store_memory_test.go +++ b/store_memory_test.go @@ -25,7 +25,7 @@ func TestSetGet(t *testing.T) { s := NewMemoryStore(GCLimitNumber, Expiration) id := "captcha id" d := "random-string" - s.Set(id, d) + _ = s.Set(id, d) d2 := s.Get(id, false) if d2 != d { t.Errorf("saved %v, getDigits returned got %v", d, d2) @@ -36,7 +36,7 @@ func TestGetClear(t *testing.T) { s := NewMemoryStore(GCLimitNumber, Expiration) id := "captcha id" d := "932839jfffjkdss" - s.Set(id, d) + _ = s.Set(id, d) d2 := s.Get(id, true) if d != d2 { t.Errorf("saved %v, getDigitsClear returned got %v", d, d2) @@ -58,7 +58,7 @@ func BenchmarkSetCollect(b *testing.B) { b.StartTimer() for i := 0; i < b.N; i++ { for j := 0; j < 1000; j++ { - s.Set(ids[j], d) + _ = s.Set(ids[j], d) } } } @@ -66,14 +66,14 @@ func BenchmarkSetCollect(b *testing.B) { func TestMemoryStore_SetGoCollect(t *testing.T) { s := NewMemoryStore(10, -1) for i := 0; i <= 100; i++ { - s.Set(fmt.Sprint(i), fmt.Sprint(i)) + _ = s.Set(fmt.Sprint(i), fmt.Sprint(i)) } } func TestMemoryStore_CollectNotExpire(t *testing.T) { s := NewMemoryStore(10, time.Hour) for i := 0; i < 50; i++ { - s.Set(fmt.Sprint(i), fmt.Sprint(i)) + _ = s.Set(fmt.Sprint(i), fmt.Sprint(i)) } // let background goroutine to go @@ -121,14 +121,14 @@ func Test_memoryStore_Set(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.s.Set(tt.args.id, tt.args.value) + _ = tt.s.Set(tt.args.id, tt.args.value) }) } } func Test_memoryStore_Verify(t *testing.T) { thisStore := NewMemoryStore(10, time.Hour) - thisStore.Set("xx", "xx") + _ = thisStore.Set("xx", "xx") got := thisStore.Verify("xx", "xx", false) if !got { t.Error("failed1") @@ -147,7 +147,7 @@ func Test_memoryStore_Verify(t *testing.T) { func Test_memoryStore_Get(t *testing.T) { thisStore := NewMemoryStore(10, time.Hour) - thisStore.Set("xx", "xx") + _ = thisStore.Set("xx", "xx") got := thisStore.Get("xx", false) if got != "xx" { t.Error("failed1") From f1c692d2c7d8fe5fc3540a7e8625efd01fd8f430 Mon Sep 17 00:00:00 2001 From: xiaohan <95028555@qq.com> Date: Mon, 19 Jul 2021 09:12:05 +0800 Subject: [PATCH 2/2] fix interface set not return error --- captcha.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/captcha.go b/captcha.go index 9906944..4fac1c4 100644 --- a/captcha.go +++ b/captcha.go @@ -36,7 +36,10 @@ func (c *Captcha) Generate() (id, b64s string, err error) { if err != nil { return "", "", err } - c.Store.Set(id, answer) + err = c.Store.Set(id, answer) + if err != nil { + return "", "", err + } b64s = item.EncodeB64string() return }