63 lines
1.5 KiB
Go
63 lines
1.5 KiB
Go
/*
|
|
* Copyright (C) 2020-2022, IrineSistiana
|
|
*
|
|
* This file is part of mosdns.
|
|
*
|
|
* mosdns is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* mosdns is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package cache
|
|
|
|
import (
|
|
"bytes"
|
|
"github.com/miekg/dns"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func Test_cachePlugin_Dump(t *testing.T) {
|
|
c := NewCache(&Args{Size: 16 * dumpBlockSize}, Opts{}) // Big enough to create dump fragments.
|
|
|
|
resp := new(dns.Msg)
|
|
resp.SetQuestion("test.", dns.TypeA)
|
|
|
|
now := time.Now()
|
|
hourLater := now.Add(time.Hour)
|
|
v := &item{
|
|
resp: resp,
|
|
storedTime: now,
|
|
expirationTime: hourLater,
|
|
}
|
|
|
|
// Fill the cache
|
|
for i := 0; i < 32*dumpBlockSize; i++ {
|
|
c.backend.Store(key(strconv.Itoa(i)), v, hourLater)
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
enw, err := c.writeDump(buf)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
enr, err := c.readDump(buf)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if enw != enr {
|
|
t.Fatalf("read err, wrote %d entries, read %d", enw, enr)
|
|
}
|
|
}
|