Refactor oracle in module and add test

This commit is contained in:
Blallo 2021-01-17 20:07:40 +01:00
parent fa972455b3
commit 5a199eec6b
No known key found for this signature in database
GPG key ID: 0CBE577C9B72DC3F
4 changed files with 120 additions and 72 deletions

View file

@ -1,8 +1,9 @@
package fs
import (
"crypto/md5"
"fmt"
"os"
"sort"
"strings"
"time"
)
@ -51,11 +52,17 @@ func (m *MailFile) SetUid(uid int) {
m.uid = uid
}
func (m *MailFile) SetMd5(md5 string) {
m.md5 = md5
func (m *MailFile) SetMd5(md5sum string) {
m.md5 = md5sum
}
func (m *MailFile) SetMd5FromName(name string) {
md5sum := md5.Sum([]byte(name))
m.SetMd5(fmt.Sprintf("%x", md5sum))
}
func (m *MailFile) SetFlags(flags []Flag) {
sort.Slice(flags, func(i, j int) bool { return flags[i] < flags[j] })
m.flags = flags
}
@ -72,54 +79,6 @@ func (m *MailFile) String() string {
)
}
type MailFileRequest struct {
respCh chan MailFile
}
func NewMailFileRequest() MailFileRequest {
ch := make(chan MailFile)
return MailFileRequest{respCh: ch}
}
func (r MailFileRequest) Response() MailFile {
return <-r.respCh
}
func NewOracle() chan MailFileRequest {
r := realClock{}
return newOracle(r)
}
func newOracle(c clock) chan MailFileRequest {
reqCh := make(chan MailFileRequest, 10)
go spawnOracle(reqCh, c)
return reqCh
}
func spawnOracle(reqCh <-chan MailFileRequest, c clock) {
count := 0
C := c.Ticker().getC()
pid := os.Getpid()
hostname, err := os.Hostname()
if err != nil {
hostname = "MISSING"
}
for {
select {
case <-C:
count = 0
case req := <-reqCh:
req.respCh <- MailFile{
timestamp: c.Now(),
progressive: count,
pid: pid,
hostname: hostname,
}
count += 1
}
}
}
type MailDir struct {
basePath string
}

View file

@ -1,35 +1,36 @@
package fs
import (
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestMailFileString(t *testing.T) {
var message_nums = []int{0, 1, 2, 3, 4}
var results []MailFile
clock := mockClock{}
oracle := newOracle(clock)
for range message_nums {
req := NewMailFileRequest()
oracle <- req
results = append(results, req.Response())
}
for i := range message_nums[1:] {
timestamp_c := results[0].timestamp != results[i].timestamp
pid_c := results[0].pid != results[i].pid
hostname_c := results[0].hostname != results[i].hostname
timestamp := clock.Now()
progressive := 4
pid := 1312
hostname := "myplace"
name := "main_account" // known md5sum: f2cf513ad46d4d9b9684103e468803a0
uid := 33243
mf := &MailFile{timestamp: timestamp, progressive: progressive, pid: pid, hostname: hostname}
mf.SetFlags([]Flag{Seen, Answered})
mf.SetMd5FromName(name)
mf.SetUid(uid)
if timestamp_c || pid_c || hostname_c {
t.Logf("timestamp_c: %+v\n", timestamp_c)
t.Logf("pid_c: %+v\n", pid_c)
t.Logf("hostname_c: %+v\n", hostname_c)
t.Errorf("Mismatching timestamps, %+v\n", cmp.Diff(results[0], results[i], cmp.AllowUnexported(MailFile{})))
}
expect := fmt.Sprintf(
"%d_%d.%d.%s,U=%d,FMD5=f2cf513ad46d4d9b9684103e468803a0:2,RS",
timestamp.Unix(),
progressive,
pid,
hostname,
uid)
result := fmt.Sprint(mf)
if results[i].progressive != i {
t.Errorf("Unexpected progressive -> have: %d | expect: %d\n", results[i].progressive, i)
}
if expect != result {
t.Errorf("Mismatch\n\tExpect: %s\n\tResult: %s\n", expect, result)
t.Logf("diff: %s", cmp.Diff(expect, result))
}
}

53
fs/oracle.go Normal file
View file

@ -0,0 +1,53 @@
package fs
import (
"os"
)
type MailFileRequest struct {
respCh chan MailFile
}
func NewMailFileRequest() MailFileRequest {
ch := make(chan MailFile)
return MailFileRequest{respCh: ch}
}
func (r MailFileRequest) Response() MailFile {
return <-r.respCh
}
func NewOracle() chan MailFileRequest {
r := realClock{}
return newOracle(r)
}
func newOracle(c clock) chan MailFileRequest {
reqCh := make(chan MailFileRequest, 10)
go spawnOracle(reqCh, c)
return reqCh
}
func spawnOracle(reqCh <-chan MailFileRequest, c clock) {
count := 0
C := c.Ticker().getC()
pid := os.Getpid()
hostname, err := os.Hostname()
if err != nil {
hostname = "MISSING"
}
for {
select {
case <-C:
count = 0
case req := <-reqCh:
req.respCh <- MailFile{
timestamp: c.Now(),
progressive: count,
pid: pid,
hostname: hostname,
}
count += 1
}
}
}

35
fs/oracle_test.go Normal file
View file

@ -0,0 +1,35 @@
package fs
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestOracle(t *testing.T) {
var message_nums = []int{0, 1, 2, 3, 4}
var results []MailFile
clock := mockClock{}
oracle := newOracle(clock)
for range message_nums {
req := NewMailFileRequest()
oracle <- req
results = append(results, req.Response())
}
for i := range message_nums[1:] {
timestamp_c := results[0].timestamp != results[i].timestamp
pid_c := results[0].pid != results[i].pid
hostname_c := results[0].hostname != results[i].hostname
if timestamp_c || pid_c || hostname_c {
t.Logf("timestamp_c: %+v\n", timestamp_c)
t.Logf("pid_c: %+v\n", pid_c)
t.Logf("hostname_c: %+v\n", hostname_c)
t.Errorf("Mismatching timestamps, %+v\n", cmp.Diff(results[0], results[i], cmp.AllowUnexported(MailFile{})))
}
if results[i].progressive != i {
t.Errorf("Unexpected progressive -> have: %d | expect: %d\n", results[i].progressive, i)
}
}
}