test: use clientmanager to logout fakeapi
This commit is contained in:
parent
99635cd56d
commit
e027aa5fae
|
@ -65,6 +65,8 @@ type Bridge struct {
|
|||
|
||||
lock sync.RWMutex
|
||||
|
||||
cancel chan struct{}
|
||||
|
||||
userAgentClientName string
|
||||
userAgentClientVersion string
|
||||
userAgentOS string
|
||||
|
@ -92,6 +94,7 @@ func New(
|
|||
storeCache: store.NewCache(config.GetIMAPCachePath()),
|
||||
idleUpdates: make(chan interface{}),
|
||||
lock: sync.RWMutex{},
|
||||
cancel: make(chan struct{}),
|
||||
}
|
||||
|
||||
// Allow DoH before starting bridge if the user has previously set this setting.
|
||||
|
@ -110,6 +113,8 @@ func New(
|
|||
b.watchAPIAuths()
|
||||
}()
|
||||
|
||||
go b.heartbeat()
|
||||
|
||||
if b.credStorer == nil {
|
||||
log.Error("Bridge has no credentials store")
|
||||
} else if err := b.loadUsersFromCredentialsStore(); err != nil {
|
||||
|
@ -120,23 +125,29 @@ func New(
|
|||
b.SendMetric(metrics.New(metrics.Setup, metrics.FirstStart, metrics.Label(version)))
|
||||
}
|
||||
|
||||
go b.heartbeat()
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
// heartbeat sends a heartbeat signal once a day.
|
||||
func (b *Bridge) heartbeat() {
|
||||
for range time.NewTicker(1 * time.Hour).C {
|
||||
next, err := strconv.ParseInt(b.pref.Get(preferences.NextHeartbeatKey), 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
nextTime := time.Unix(next, 0)
|
||||
if time.Now().After(nextTime) {
|
||||
b.SendMetric(metrics.New(metrics.Heartbeat, metrics.Daily, metrics.NoLabel))
|
||||
nextTime = nextTime.Add(24 * time.Hour)
|
||||
b.pref.Set(preferences.NextHeartbeatKey, strconv.FormatInt(nextTime.Unix(), 10))
|
||||
ticker := time.NewTicker(1 * time.Minute)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
next, err := strconv.ParseInt(b.pref.Get(preferences.NextHeartbeatKey), 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
nextTime := time.Unix(next, 0)
|
||||
if time.Now().After(nextTime) {
|
||||
b.SendMetric(metrics.New(metrics.Heartbeat, metrics.Daily, metrics.NoLabel))
|
||||
nextTime = nextTime.Add(24 * time.Hour)
|
||||
b.pref.Set(preferences.NextHeartbeatKey, strconv.FormatInt(nextTime.Unix(), 10))
|
||||
}
|
||||
|
||||
case <-b.cancel:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -171,30 +182,44 @@ func (b *Bridge) loadUsersFromCredentialsStore() (err error) {
|
|||
|
||||
func (b *Bridge) watchBridgeOutdated() {
|
||||
ch := make(chan string)
|
||||
|
||||
b.events.Add(events.UpgradeApplicationEvent, ch)
|
||||
for range ch {
|
||||
isApplicationOutdated = true
|
||||
b.closeAllConnections()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ch:
|
||||
isApplicationOutdated = true
|
||||
b.closeAllConnections()
|
||||
|
||||
case <-b.cancel:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// watchAPIAuths receives auths from the client manager and sends them to the appropriate user.
|
||||
func (b *Bridge) watchAPIAuths() {
|
||||
for auth := range b.clientManager.GetAuthUpdateChannel() {
|
||||
log.Debug("Bridge received auth from ClientManager")
|
||||
for {
|
||||
select {
|
||||
case auth := <-b.clientManager.GetAuthUpdateChannel():
|
||||
log.Debug("Bridge received auth from ClientManager")
|
||||
|
||||
user, ok := b.hasUser(auth.UserID)
|
||||
if !ok {
|
||||
log.WithField("userID", auth.UserID).Info("User not available for auth update")
|
||||
continue
|
||||
}
|
||||
user, ok := b.hasUser(auth.UserID)
|
||||
if !ok {
|
||||
log.WithField("userID", auth.UserID).Info("User not available for auth update")
|
||||
continue
|
||||
}
|
||||
|
||||
if auth.Auth != nil {
|
||||
user.updateAuthToken(auth.Auth)
|
||||
} else if err := user.logout(); err != nil {
|
||||
log.WithError(err).
|
||||
WithField("userID", auth.UserID).
|
||||
Error("User logout failed while watching API auths")
|
||||
if auth.Auth != nil {
|
||||
user.updateAuthToken(auth.Auth)
|
||||
} else if err := user.logout(); err != nil {
|
||||
log.WithError(err).
|
||||
WithField("userID", auth.UserID).
|
||||
Error("User logout failed while watching API auths")
|
||||
}
|
||||
|
||||
case <-b.cancel:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -541,6 +566,11 @@ func (b *Bridge) CheckConnection() error {
|
|||
return b.clientManager.CheckConnection()
|
||||
}
|
||||
|
||||
// StopWatchers stops all bridge goroutines.
|
||||
func (b *Bridge) StopWatchers() {
|
||||
close(b.cancel)
|
||||
}
|
||||
|
||||
func (b *Bridge) updateCurrentUserAgent() {
|
||||
UpdateCurrentUserAgent(b.version, b.userAgentOS, b.userAgentClientName, b.userAgentClientVersion)
|
||||
}
|
||||
|
|
|
@ -49,7 +49,10 @@ func (ctx *TestContext) RestartBridge() error {
|
|||
_ = user.GetStore().Close()
|
||||
}
|
||||
|
||||
ctx.bridge.StopWatchers()
|
||||
|
||||
ctx.withBridgeInstance()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -148,10 +148,7 @@ func (api *FakePMAPI) AuthRefresh(token string) (*pmapi.Auth, error) {
|
|||
}
|
||||
|
||||
func (api *FakePMAPI) Logout() {
|
||||
if err := api.DeleteAuth(); err != nil {
|
||||
api.log.WithError(err).Error("delete auth failed during logout")
|
||||
}
|
||||
api.ClearData()
|
||||
api.controller.clientManager.LogoutClient(api.userID)
|
||||
}
|
||||
|
||||
func (api *FakePMAPI) IsConnected() bool {
|
||||
|
@ -162,8 +159,6 @@ func (api *FakePMAPI) DeleteAuth() error {
|
|||
if err := api.checkAndRecordCall(DELETE, "/auth", nil); err != nil {
|
||||
return err
|
||||
}
|
||||
// Logout will also emit change to auth channel
|
||||
api.sendAuth(nil)
|
||||
api.controller.deleteSession(api.uid)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ func NewController(cm *pmapi.ClientManager) *Controller {
|
|||
}
|
||||
|
||||
cm.SetClientConstructor(func(userID string) pmapi.Client {
|
||||
fakeAPI := New(controller)
|
||||
fakeAPI := New(controller, userID)
|
||||
controller.fakeAPIs = append(controller.fakeAPIs, fakeAPI)
|
||||
return fakeAPI
|
||||
})
|
||||
|
|
|
@ -29,6 +29,7 @@ var errBadRequest = errors.New("NOT OK: 400 Bad Request")
|
|||
|
||||
type FakePMAPI struct {
|
||||
username string
|
||||
userID string
|
||||
controller *Controller
|
||||
eventIDGenerator idGenerator
|
||||
|
||||
|
@ -45,16 +46,19 @@ type FakePMAPI struct {
|
|||
log *logrus.Entry
|
||||
}
|
||||
|
||||
func New(controller *Controller) *FakePMAPI {
|
||||
func New(controller *Controller, userID string) *FakePMAPI {
|
||||
fakePMAPI := &FakePMAPI{
|
||||
controller: controller,
|
||||
log: logrus.WithField("pkg", "fakeapi"),
|
||||
userID: userID,
|
||||
}
|
||||
|
||||
fakePMAPI.addEvent(&pmapi.Event{
|
||||
EventID: fakePMAPI.eventIDGenerator.last("event"),
|
||||
Refresh: 0,
|
||||
More: 0,
|
||||
})
|
||||
|
||||
return fakePMAPI
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue