Add some test cases to avoid breaking CanSee with GroupStore.Reload again.

Shorten some variable names.
This commit is contained in:
Azareal 2019-06-18 20:02:45 +10:00
parent 768406f9ae
commit 54abbe980f
3 changed files with 79 additions and 42 deletions

View File

@ -43,19 +43,19 @@ func NewMemoryForumPermsStore() (*MemoryForumPermsStore, error) {
}, acc.FirstError() }, acc.FirstError()
} }
func (fps *MemoryForumPermsStore) Init() error { func (s *MemoryForumPermsStore) Init() error {
DebugLog("Initialising the forum perms store") DebugLog("Initialising the forum perms store")
return fps.ReloadAll() return s.ReloadAll()
} }
func (fps *MemoryForumPermsStore) ReloadAll() error { func (s *MemoryForumPermsStore) ReloadAll() error {
DebugLog("Reloading the forum perms") DebugLog("Reloading the forum perms")
fids, err := Forums.GetAllIDs() fids, err := Forums.GetAllIDs()
if err != nil { if err != nil {
return err return err
} }
for _, fid := range fids { for _, fid := range fids {
err := fps.Reload(fid) err := s.Reload(fid)
if err != nil { if err != nil {
return err return err
} }
@ -63,7 +63,7 @@ func (fps *MemoryForumPermsStore) ReloadAll() error {
return nil return nil
} }
func (fps *MemoryForumPermsStore) parseForumPerm(perms []byte) (pperms *ForumPerms, err error) { func (s *MemoryForumPermsStore) parseForumPerm(perms []byte) (pperms *ForumPerms, err error) {
DebugDetail("perms: ", string(perms)) DebugDetail("perms: ", string(perms))
pperms = BlankForumPerms() pperms = BlankForumPerms()
err = json.Unmarshal(perms, &pperms) err = json.Unmarshal(perms, &pperms)
@ -73,9 +73,9 @@ func (fps *MemoryForumPermsStore) parseForumPerm(perms []byte) (pperms *ForumPer
} }
// TODO: Need a more thread-safe way of doing this. Possibly with sync.Map? // TODO: Need a more thread-safe way of doing this. Possibly with sync.Map?
func (fps *MemoryForumPermsStore) Reload(fid int) error { func (s *MemoryForumPermsStore) Reload(fid int) error {
DebugLogf("Reloading the forum permissions for forum #%d", fid) DebugLogf("Reloading the forum permissions for forum #%d", fid)
rows, err := fps.getByForum.Query(fid) rows, err := s.getByForum.Query(fid)
if err != nil { if err != nil {
return err return err
} }
@ -92,7 +92,7 @@ func (fps *MemoryForumPermsStore) Reload(fid int) error {
DebugLog("gid: ", gid) DebugLog("gid: ", gid)
DebugLogf("perms: %+v\n", perms) DebugLogf("perms: %+v\n", perms)
pperms, err := fps.parseForumPerm(perms) pperms, err := s.parseForumPerm(perms)
if err != nil { if err != nil {
return err return err
} }
@ -101,13 +101,13 @@ func (fps *MemoryForumPermsStore) Reload(fid int) error {
} }
DebugLogf("forumPerms: %+v\n", forumPerms) DebugLogf("forumPerms: %+v\n", forumPerms)
if fid%2 == 0 { if fid%2 == 0 {
fps.evenLock.Lock() s.evenLock.Lock()
fps.evenForums[fid] = forumPerms s.evenForums[fid] = forumPerms
fps.evenLock.Unlock() s.evenLock.Unlock()
} else { } else {
fps.oddLock.Lock() s.oddLock.Lock()
fps.oddForums[fid] = forumPerms s.oddForums[fid] = forumPerms
fps.oddLock.Unlock() s.oddLock.Unlock()
} }
groups, err := Groups.GetAll() groups, err := Groups.GetAll()
@ -127,13 +127,13 @@ func (fps *MemoryForumPermsStore) Reload(fid int) error {
var forumPerms map[int]*ForumPerms var forumPerms map[int]*ForumPerms
var ok bool var ok bool
if fid%2 == 0 { if fid%2 == 0 {
fps.evenLock.RLock() s.evenLock.RLock()
forumPerms, ok = fps.evenForums[fid] forumPerms, ok = s.evenForums[fid]
fps.evenLock.RUnlock() s.evenLock.RUnlock()
} else { } else {
fps.oddLock.RLock() s.oddLock.RLock()
forumPerms, ok = fps.oddForums[fid] forumPerms, ok = s.oddForums[fid]
fps.oddLock.RUnlock() s.oddLock.RUnlock()
} }
var forumPerm *ForumPerms var forumPerm *ForumPerms

View File

@ -92,16 +92,16 @@ func NewMemoryForumStore() (*MemoryForumStore, error) {
// TODO: Rename to ReloadAll? // TODO: Rename to ReloadAll?
// TODO: Add support for subforums // TODO: Add support for subforums
func (mfs *MemoryForumStore) LoadForums() error { func (s *MemoryForumStore) LoadForums() error {
var forumView []*Forum var forumView []*Forum
addForum := func(forum *Forum) { addForum := func(forum *Forum) {
mfs.forums.Store(forum.ID, forum) s.forums.Store(forum.ID, forum)
if forum.Active && forum.Name != "" && forum.ParentType == "" { if forum.Active && forum.Name != "" && forum.ParentType == "" {
forumView = append(forumView, forum) forumView = append(forumView, forum)
} }
} }
rows, err := mfs.getAll.Query() rows, err := s.getAll.Query()
if err != nil { if err != nil {
return err return err
} }
@ -109,25 +109,24 @@ func (mfs *MemoryForumStore) LoadForums() error {
var i = 0 var i = 0
for ; rows.Next(); i++ { for ; rows.Next(); i++ {
forum := &Forum{ID: 0, Active: true, Preset: "all"} f := &Forum{ID: 0, Active: true, Preset: "all"}
err = rows.Scan(&forum.ID, &forum.Name, &forum.Desc, &forum.Tmpl, &forum.Active, &forum.Order, &forum.Preset, &forum.ParentID, &forum.ParentType, &forum.TopicCount, &forum.LastTopicID, &forum.LastReplyerID) err = rows.Scan(&f.ID, &f.Name, &f.Desc, &f.Tmpl, &f.Active, &f.Order, &f.Preset, &f.ParentID, &f.ParentType, &f.TopicCount, &f.LastTopicID, &f.LastReplyerID)
if err != nil { if err != nil {
return err return err
} }
if forum.Name == "" { if f.Name == "" {
DebugLog("Adding a placeholder forum") DebugLog("Adding a placeholder forum")
} else { } else {
log.Printf("Adding the '%s' forum", forum.Name) log.Printf("Adding the '%s' forum", f.Name)
} }
forum.Link = BuildForumURL(NameToSlug(forum.Name), forum.ID) f.Link = BuildForumURL(NameToSlug(f.Name), f.ID)
forum.LastTopic = Topics.DirtyGet(forum.LastTopicID) f.LastTopic = Topics.DirtyGet(f.LastTopicID)
forum.LastReplyer = Users.DirtyGet(forum.LastReplyerID) f.LastReplyer = Users.DirtyGet(f.LastReplyerID)
addForum(f)
addForum(forum)
} }
mfs.forumView.Store(forumView) s.forumView.Store(forumView)
TopicListThaw.Thaw() TopicListThaw.Thaw()
return rows.Err() return rows.Err()
} }
@ -184,20 +183,20 @@ func (s *MemoryForumStore) Get(id int) (*Forum, error) {
} }
func (s *MemoryForumStore) BypassGet(id int) (*Forum, error) { func (s *MemoryForumStore) BypassGet(id int) (*Forum, error) {
var forum = &Forum{ID: id} var f = &Forum{ID: id}
err := s.get.QueryRow(id).Scan(&forum.Name, &forum.Desc, &forum.Tmpl,&forum.Active, &forum.Order, &forum.Preset, &forum.ParentID, &forum.ParentType, &forum.TopicCount, &forum.LastTopicID, &forum.LastReplyerID) err := s.get.QueryRow(id).Scan(&f.Name, &f.Desc, &f.Tmpl,&f.Active, &f.Order, &f.Preset, &f.ParentID, &f.ParentType, &f.TopicCount, &f.LastTopicID, &f.LastReplyerID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if forum.Name == "" { if f.Name == "" {
return nil, ErrNoRows return nil, ErrNoRows
} }
forum.Link = BuildForumURL(NameToSlug(forum.Name), forum.ID) f.Link = BuildForumURL(NameToSlug(f.Name), f.ID)
forum.LastTopic = Topics.DirtyGet(forum.LastTopicID) f.LastTopic = Topics.DirtyGet(f.LastTopicID)
forum.LastReplyer = Users.DirtyGet(forum.LastReplyerID) f.LastReplyer = Users.DirtyGet(f.LastReplyerID)
//TopicListThaw.Thaw() //TopicListThaw.Thaw()
return forum, err return f, err
} }
// TODO: Optimise this // TODO: Optimise this
@ -263,10 +262,10 @@ func (s *MemoryForumStore) GetAllVisibleIDs() ([]int, error) {
} }
// TODO: Implement sub-forums. // TODO: Implement sub-forums.
/*func (mfs *MemoryForumStore) GetChildren(parentID int, parentType string) ([]*Forum,error) { /*func (s *MemoryForumStore) GetChildren(parentID int, parentType string) ([]*Forum,error) {
return nil, nil return nil, nil
} }
func (mfs *MemoryForumStore) GetFirstChild(parentID int, parentType string) (*Forum,error) { func (s *MemoryForumStore) GetFirstChild(parentID int, parentType string) (*Forum,error) {
return nil, nil return nil, nil
}*/ }*/

View File

@ -8,6 +8,7 @@ import (
"strconv" "strconv"
"testing" "testing"
"time" "time"
"database/sql"
c "github.com/Azareal/Gosora/common" c "github.com/Azareal/Gosora/common"
"github.com/Azareal/Gosora/common/phrases" "github.com/Azareal/Gosora/common/phrases"
@ -752,6 +753,7 @@ func TestGroupStore(t *testing.T) {
group, err = c.Groups.Get(1) group, err = c.Groups.Get(1)
recordMustExist(t, err, "Couldn't find GID #1") recordMustExist(t, err, "Couldn't find GID #1")
expect(t, group.ID == 1, fmt.Sprintf("group.ID doesn't not match the requested GID. Got '%d' instead.'", group.ID)) expect(t, group.ID == 1, fmt.Sprintf("group.ID doesn't not match the requested GID. Got '%d' instead.'", group.ID))
expect(t,len(group.CanSee) > 0,"group.CanSee should not be zero")
expect(t, !c.Groups.Exists(-1), "GID #-1 shouldn't exist") expect(t, !c.Groups.Exists(-1), "GID #-1 shouldn't exist")
// 0 aka Unknown, for system posts and other oddities // 0 aka Unknown, for system posts and other oddities
@ -771,6 +773,7 @@ func TestGroupStore(t *testing.T) {
expect(t, group.IsAdmin, "This should be an admin group") expect(t, group.IsAdmin, "This should be an admin group")
expect(t, group.IsMod, "This should be a mod group") expect(t, group.IsMod, "This should be a mod group")
expect(t, !group.IsBanned, "This shouldn't be a ban group") expect(t, !group.IsBanned, "This shouldn't be a ban group")
// TODO: Add a proper CanSee test
isAdmin = false isAdmin = false
isMod = true isMod = true
@ -806,16 +809,33 @@ func TestGroupStore(t *testing.T) {
expect(t, group.IsAdmin, "This should be an admin group") expect(t, group.IsAdmin, "This should be an admin group")
expect(t, group.IsMod, "This should be a mod group") expect(t, group.IsMod, "This should be a mod group")
expect(t, !group.IsBanned, "This shouldn't be a ban group") expect(t, !group.IsBanned, "This shouldn't be a ban group")
expect(t, len(group.CanSee) == 0, "len(group.CanSee) should be 0")
err = group.ChangeRank(false, true, true) err = group.ChangeRank(false, true, true)
expectNilErr(t, err) expectNilErr(t, err)
forum, err := c.Forums.Get(2)
expectNilErr(t, err)
forumPerms, err := c.FPStore.GetCopy(2, gid)
if err == sql.ErrNoRows {
forumPerms = *c.BlankForumPerms()
} else if err != nil {
expectNilErr(t, err)
}
forumPerms.ViewTopic = true
err = forum.SetPerms(&forumPerms, "custom", gid)
expectNilErr(t, err)
group, err = c.Groups.Get(gid) group, err = c.Groups.Get(gid)
expectNilErr(t, err) expectNilErr(t, err)
expect(t, group.ID == gid, "The group ID should match the requested ID") expect(t, group.ID == gid, "The group ID should match the requested ID")
expect(t, !group.IsAdmin, "This shouldn't be an admin group") expect(t, !group.IsAdmin, "This shouldn't be an admin group")
expect(t, group.IsMod, "This should be a mod group") expect(t, group.IsMod, "This should be a mod group")
expect(t, !group.IsBanned, "This shouldn't be a ban group") expect(t, !group.IsBanned, "This shouldn't be a ban group")
expect(t, group.CanSee!=nil, "group.CanSee must not be nil")
expect(t, len(group.CanSee) > 0, "group.CanSee should not be zero")
canSee := group.CanSee
// Make sure the data is static // Make sure the data is static
c.Groups.Reload(gid) c.Groups.Reload(gid)
@ -827,6 +847,24 @@ func TestGroupStore(t *testing.T) {
expect(t, group.IsMod, "This should be a mod group") expect(t, group.IsMod, "This should be a mod group")
expect(t, !group.IsBanned, "This shouldn't be a ban group") expect(t, !group.IsBanned, "This shouldn't be a ban group")
// TODO: Don't enforce a specific order here
canSeeTest := func(a, b []int) bool {
if (a == nil) != (b == nil) {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
expect(t, canSeeTest(group.CanSee,canSee), "group.CanSee is not being reused")
// TODO: Test group deletion // TODO: Test group deletion
// TODO: Test group reload // TODO: Test group reload
// TODO: Test group cache set // TODO: Test group cache set