Test message logging

This commit is contained in:
Ken-Håvard Lieng 2016-01-17 21:15:29 +01:00
parent d023f63a7c
commit 1def24500a
5 changed files with 105 additions and 42 deletions

View File

@ -49,8 +49,8 @@ func handleAuth(w http.ResponseWriter, r *http.Request) *Session {
} }
func newUser(w http.ResponseWriter, r *http.Request) *Session { func newUser(w http.ResponseWriter, r *http.Request) *Session {
user := storage.NewUser() user, err := storage.NewUser()
if user == nil { if err != nil {
return nil return nil
} }

View File

@ -22,7 +22,10 @@ func TestMain(m *testing.M) {
storage.Initialize(tempdir) storage.Initialize(tempdir)
storage.Open() storage.Open()
user = storage.NewUser() user, err = storage.NewUser()
if err != nil {
os.Exit(1)
}
channelStore = storage.NewChannelStore() channelStore = storage.NewChannelStore()
code := m.Run() code := m.Run()

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"log"
"strconv" "strconv"
"sync" "sync"
@ -41,39 +40,34 @@ type Channel struct {
Topic string `json:"topic,omitempty"` Topic string `json:"topic,omitempty"`
} }
func NewUser() *User { func NewUser() (*User, error) {
user := &User{} user := &User{}
err := db.Update(func(tx *bolt.Tx) error { err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(bucketUsers) b := tx.Bucket(bucketUsers)
var err error user.ID, _ = b.NextSequence()
user.ID, err = b.NextSequence()
if err != nil {
return err
}
user.Username = strconv.FormatUint(user.ID, 10) user.Username = strconv.FormatUint(user.ID, 10)
user.id = idToBytes(user.ID)
data, err := json.Marshal(user) data, err := json.Marshal(user)
if err != nil { if err != nil {
return err return err
} }
user.id = idToBytes(user.ID)
return b.Put(user.id, data) return b.Put(user.id, data)
}) })
if err != nil { if err != nil {
return nil return nil, err
} }
err = user.openMessageLog() err = user.openMessageLog()
if err != nil { if err != nil {
log.Println(err) return nil, err
} }
return user return user, nil
} }
func LoadUsers() []*User { func LoadUsers() []*User {

View File

@ -15,38 +15,42 @@ type Message struct {
ID uint64 `json:"id"` ID uint64 `json:"id"`
Server string `json:"server"` Server string `json:"server"`
From string `json:"from"` From string `json:"from"`
To string `json:"to"` To string `json:"to,omitempty"`
Content string `json:"content"` Content string `json:"content"`
Time int64 `json:"time"` Time int64 `json:"time"`
} }
func (u *User) LogMessage(server, from, to, content string) { func (u *User) LogMessage(server, from, to, content string) error {
message := Message{
Server: server,
From: from,
To: to,
Content: content,
Time: time.Now().Unix(),
}
bucketKey := server + ":" + to bucketKey := server + ":" + to
var id uint64
var idStr string
var message Message
u.messageLog.Update(func(tx *bolt.Tx) error { err := u.messageLog.Batch(func(tx *bolt.Tx) error {
b, _ := tx.Bucket(bucketMessages).CreateBucketIfNotExists([]byte(bucketKey)) b, err := tx.Bucket(bucketMessages).CreateBucketIfNotExists([]byte(bucketKey))
id, _ = b.NextSequence() if err != nil {
idStr = strconv.FormatUint(id, 10) return err
message = Message{
ID: id,
Content: content,
Server: server,
From: from,
To: to,
Time: time.Now().Unix(),
} }
data, _ := json.Marshal(message) message.ID, _ = b.NextSequence()
b.Put([]byte(idStr), data)
return nil data, err := json.Marshal(message)
if err != nil {
return err
}
return b.Put(idToBytes(message.ID), data)
}) })
u.messageIndex.Index(bucketKey+":"+idStr, message) if err != nil {
return err
}
return u.messageIndex.Index(bucketKey+":"+strconv.FormatUint(message.ID, 10), message)
} }
func (u *User) GetLastMessages(server, channel string, count int) ([]Message, error) { func (u *User) GetLastMessages(server, channel string, count int) ([]Message, error) {
@ -60,7 +64,7 @@ func (u *User) GetLastMessages(server, channel string, count int) ([]Message, er
c := b.Cursor() c := b.Cursor()
for k, v := c.Last(); count > 0 && k != nil; k, v = c.Prev() { for _, v := c.Last(); count > 0 && v != nil; _, v = c.Prev() {
count-- count--
json.Unmarshal(v, &messages[count]) json.Unmarshal(v, &messages[count])
} }
@ -85,7 +89,7 @@ func (u *User) GetMessages(server, channel string, count int, fromID uint64) ([]
} }
c := b.Cursor() c := b.Cursor()
c.Seek([]byte(strconv.FormatUint(fromID, 10))) c.Seek(idToBytes(fromID))
for k, v := c.Prev(); count > 0 && k != nil; k, v = c.Prev() { for k, v := c.Prev(); count > 0 && k != nil; k, v = c.Prev() {
count-- count--
@ -107,7 +111,7 @@ func (u *User) SearchMessages(server, channel, phrase string) ([]Message, error)
serverQuery.SetField("server") serverQuery.SetField("server")
channelQuery := bleve.NewMatchQuery(channel) channelQuery := bleve.NewMatchQuery(channel)
channelQuery.SetField("to") channelQuery.SetField("to")
contentQuery := bleve.NewMatchQuery(phrase) contentQuery := bleve.NewFuzzyQuery(phrase)
contentQuery.SetField("content") contentQuery.SetField("content")
query := bleve.NewBooleanQuery([]bleve.Query{serverQuery, channelQuery, contentQuery}, nil, nil) query := bleve.NewBooleanQuery([]bleve.Query{serverQuery, channelQuery, contentQuery}, nil, nil)
@ -125,9 +129,10 @@ func (u *User) SearchMessages(server, channel, phrase string) ([]Message, error)
for _, hit := range searchResults.Hits { for _, hit := range searchResults.Hits {
idx := strings.LastIndex(hit.ID, ":") idx := strings.LastIndex(hit.ID, ":")
bc := b.Bucket([]byte(hit.ID[:idx])) bc := b.Bucket([]byte(hit.ID[:idx]))
var message Message id, _ := strconv.ParseUint(hit.ID[idx+1:], 10, 64)
json.Unmarshal(bc.Get([]byte(hit.ID[idx+1:])), &message) var message Message
json.Unmarshal(bc.Get(idToBytes(id)), &message)
messages = append(messages, message) messages = append(messages, message)
} }

View File

@ -2,6 +2,7 @@ package storage
import ( import (
"io/ioutil" "io/ioutil"
"strconv"
"testing" "testing"
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/stretchr/testify/assert" "github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/stretchr/testify/assert"
@ -35,7 +36,8 @@ func TestUser(t *testing.T) {
Name: "#testing", Name: "#testing",
} }
user := NewUser() user, err := NewUser()
assert.Nil(t, err)
user.AddServer(srv) user.AddServer(srv)
user.AddChannel(chan1) user.AddChannel(chan1)
user.AddChannel(chan2) user.AddChannel(chan2)
@ -68,3 +70,62 @@ func TestUser(t *testing.T) {
assert.Len(t, user.GetServers(), 0) assert.Len(t, user.GetServers(), 0)
assert.Len(t, user.GetChannels(), 0) assert.Len(t, user.GetChannels(), 0)
} }
func TestMessages(t *testing.T) {
Initialize(tempdir())
Open()
user, err := NewUser()
assert.Nil(t, err)
messages, err := user.GetMessages("irc.freenode.net", "#go-nuts", 10, 6)
assert.Nil(t, err)
assert.Len(t, messages, 0)
messages, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 10)
assert.Nil(t, err)
assert.Len(t, messages, 0)
messages, err = user.SearchMessages("irc.freenode.net", "#go-nuts", "message")
assert.Nil(t, err)
assert.Len(t, messages, 0)
for i := 0; i < 5; i++ {
err = user.LogMessage("irc.freenode.net", "nick", "#go-nuts", "message"+strconv.Itoa(i))
assert.Nil(t, err)
}
messages, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, 6)
assert.Equal(t, "message0", messages[0].Content)
assert.Equal(t, "message4", messages[4].Content)
assert.Nil(t, err)
assert.Len(t, messages, 5)
messages, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, 100)
assert.Nil(t, err)
assert.Len(t, messages, 5)
messages, err = user.GetMessages("irc.freenode.net", "#go-nuts", 10, 4)
assert.Equal(t, "message0", messages[0].Content)
assert.Equal(t, "message2", messages[2].Content)
assert.Nil(t, err)
assert.Len(t, messages, 3)
messages, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 10)
assert.Equal(t, "message0", messages[0].Content)
assert.Equal(t, "message2", messages[2].Content)
assert.Nil(t, err)
assert.Len(t, messages, 5)
messages, err = user.GetLastMessages("irc.freenode.net", "#go-nuts", 3)
assert.Equal(t, "message2", messages[0].Content)
assert.Equal(t, "message4", messages[2].Content)
assert.Nil(t, err)
assert.Len(t, messages, 3)
messages, err = user.SearchMessages("irc.freenode.net", "#go-nuts", "message")
assert.Nil(t, err)
assert.Len(t, messages, 5)
Close()
}