Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion interface_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion store_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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 {
Expand Down
16 changes: 8 additions & 8 deletions store_memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -58,22 +58,22 @@ 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)
}
}
}

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
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down