From 5a199eec6bb211096b84b2cc7029a6fc73dbff8e Mon Sep 17 00:00:00 2001 From: Blallo Date: Sun, 17 Jan 2021 20:07:40 +0100 Subject: [PATCH] Refactor oracle in module and add test --- fs/maildir.go | 61 ++++++++-------------------------------------- fs/maildir_test.go | 43 ++++++++++++++++---------------- fs/oracle.go | 53 ++++++++++++++++++++++++++++++++++++++++ fs/oracle_test.go | 35 ++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 72 deletions(-) create mode 100644 fs/oracle.go create mode 100644 fs/oracle_test.go diff --git a/fs/maildir.go b/fs/maildir.go index 97131ba..a719e97 100644 --- a/fs/maildir.go +++ b/fs/maildir.go @@ -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 } diff --git a/fs/maildir_test.go b/fs/maildir_test.go index 140ef3d..1ceee36 100644 --- a/fs/maildir_test.go +++ b/fs/maildir_test.go @@ -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)) } } diff --git a/fs/oracle.go b/fs/oracle.go new file mode 100644 index 0000000..be27770 --- /dev/null +++ b/fs/oracle.go @@ -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 + } + } +} diff --git a/fs/oracle_test.go b/fs/oracle_test.go new file mode 100644 index 0000000..3555759 --- /dev/null +++ b/fs/oracle_test.go @@ -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) + } + } +}