mirror of
https://github.com/jech/galene.git
synced 2024-11-25 01:55:57 +01:00
Implement packetcache.Last.
This commit is contained in:
parent
e6bf9338dd
commit
a189e0ad46
2 changed files with 31 additions and 2 deletions
|
@ -388,6 +388,20 @@ func (cache *Cache) Get(seqno uint16, result []byte) uint16 {
|
|||
return 0
|
||||
}
|
||||
|
||||
func (cache *Cache) Last() (bool, uint16, uint32) {
|
||||
cache.mu.Lock()
|
||||
defer cache.mu.Unlock()
|
||||
if !cache.lastValid {
|
||||
return false, 0, 0
|
||||
}
|
||||
buf := make([]byte, BufSize)
|
||||
len, ts, _ := get(cache.last, cache.entries, buf)
|
||||
if len == 0 {
|
||||
return false, 0, 0
|
||||
}
|
||||
return true, cache.last, ts
|
||||
}
|
||||
|
||||
// GetAt retrieves a packet from the cache assuming it is at the given index.
|
||||
func (cache *Cache) GetAt(seqno uint16, index uint16, result []byte) uint16 {
|
||||
cache.mu.Lock()
|
||||
|
|
|
@ -20,8 +20,23 @@ func TestCache(t *testing.T) {
|
|||
buf1 := randomBuf()
|
||||
buf2 := randomBuf()
|
||||
cache := New(16)
|
||||
_, i1 := cache.Store(13, 0, false, false, buf1)
|
||||
_, i2 := cache.Store(17, 0, false, false, buf2)
|
||||
|
||||
found, _, _ := cache.Last()
|
||||
if found {
|
||||
t.Errorf("Found in empty cache")
|
||||
}
|
||||
|
||||
_, i1 := cache.Store(13, 42, false, false, buf1)
|
||||
_, i2 := cache.Store(17, 42, false, false, buf2)
|
||||
|
||||
found, seqno, ts := cache.Last()
|
||||
if !found {
|
||||
t.Errorf("Not found")
|
||||
}
|
||||
if seqno != 17 || ts != 42 {
|
||||
t.Errorf("Expected %v, %v, got %v, %v",
|
||||
17, 42, seqno, ts)
|
||||
}
|
||||
|
||||
buf := make([]byte, BufSize)
|
||||
|
||||
|
|
Loading…
Reference in a new issue