Merge branch 'master' of ssh://github.com/micro/go-micro
This commit is contained in:
		| @@ -32,10 +32,16 @@ func (m *memoryStore) Dump() ([]*store.Record, error) { | |||||||
| 		d := v.r.Expiry | 		d := v.r.Expiry | ||||||
| 		t := time.Since(v.c) | 		t := time.Since(v.c) | ||||||
|  |  | ||||||
| 		// expired | 		if d > time.Duration(0) { | ||||||
| 		if d > time.Duration(0) && t > d { | 			// expired | ||||||
| 			continue | 			if t > d { | ||||||
|  | 				continue | ||||||
|  | 			} | ||||||
|  | 			// update expiry | ||||||
|  | 			v.r.Expiry -= t | ||||||
|  | 			v.c = time.Now() | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		values = append(values, v.r) | 		values = append(values, v.r) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -56,8 +62,13 @@ func (m *memoryStore) Read(key string) (*store.Record, error) { | |||||||
| 	t := time.Since(v.c) | 	t := time.Since(v.c) | ||||||
|  |  | ||||||
| 	// expired | 	// expired | ||||||
| 	if d > time.Duration(0) && t > d { | 	if d > time.Duration(0) { | ||||||
| 		return nil, store.ErrNotFound | 		if t > d { | ||||||
|  | 			return nil, store.ErrNotFound | ||||||
|  | 		} | ||||||
|  | 		// update expiry | ||||||
|  | 		v.r.Expiry -= t | ||||||
|  | 		v.c = time.Now() | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return v.r, nil | 	return v.r, nil | ||||||
|   | |||||||
							
								
								
									
										37
									
								
								data/store/memory/memory_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								data/store/memory/memory_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | |||||||
|  | package memory | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"testing" | ||||||
|  | 	"time" | ||||||
|  |  | ||||||
|  | 	"github.com/micro/go-micro/data/store" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func TestReadRecordExpire(t *testing.T) { | ||||||
|  | 	s := NewStore() | ||||||
|  |  | ||||||
|  | 	var ( | ||||||
|  | 		key    = "foo" | ||||||
|  | 		expire = 100 * time.Millisecond | ||||||
|  | 	) | ||||||
|  | 	rec := &store.Record{ | ||||||
|  | 		Key:    key, | ||||||
|  | 		Value:  nil, | ||||||
|  | 		Expiry: expire, | ||||||
|  | 	} | ||||||
|  | 	s.Write(rec) | ||||||
|  |  | ||||||
|  | 	rrec, err := s.Read(key) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 	if rrec.Expiry >= expire { | ||||||
|  | 		t.Fatal("expiry of read record is not changed") | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	time.Sleep(expire) | ||||||
|  |  | ||||||
|  | 	if _, err := s.Read(key); err != store.ErrNotFound { | ||||||
|  | 		t.Fatal("expire elapsed, but key still accessable") | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user