Skip to content

Commit

Permalink
Refactor SetOffline to return err
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed May 13, 2024
1 parent 1f532e9 commit 0d66ab4
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ type browserContextAPI interface {
SetDefaultTimeout(timeout int64)
SetGeolocation(geolocation goja.Value) error
SetHTTPCredentials(httpCredentials goja.Value) error
SetOffline(offline bool)
SetOffline(offline bool) error
WaitForEvent(event string, optsOrPredicate goja.Value) (any, error)
}

Expand Down
11 changes: 9 additions & 2 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,20 @@ func (b *BrowserContext) SetHTTPCredentials(httpCredentials goja.Value) error {
}

// SetOffline toggles the browser's connectivity on/off.
func (b *BrowserContext) SetOffline(offline bool) {
func (b *BrowserContext) SetOffline(offline bool) error {
b.logger.Debugf("BrowserContext:SetOffline", "bctxid:%v offline:%t", b.id, offline)

b.opts.Offline = offline
for _, p := range b.browser.getPages() {
p.updateOffline()
if err := p.updateOffline(); err != nil {
return fmt.Errorf(
"setting offline status to %t for the browser context ID %s: %w",
offline, b.id, err,
)
}
}

return nil
}

// Timeout will return the default timeout or the one set by the user.
Expand Down
12 changes: 9 additions & 3 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,9 @@ func (fs *FrameSession) initOptions() error {
return err
}

fs.updateOffline(true)
if err := fs.updateOffline(true); err != nil {
return err
}
if err := fs.updateHTTPCredentials(true); err != nil {
return err
}
Expand Down Expand Up @@ -1100,13 +1102,17 @@ func (fs *FrameSession) updateHTTPCredentials(initial bool) error {
return nil
}

func (fs *FrameSession) updateOffline(initial bool) {
func (fs *FrameSession) updateOffline(initial bool) error {
fs.logger.Debugf("NewFrameSession:updateOffline", "sid:%v tid:%v", fs.session.ID(), fs.targetID)

offline := fs.page.browserCtx.opts.Offline
if !initial || offline {
fs.networkManager.SetOfflineMode(offline)
if err := fs.networkManager.SetOfflineMode(offline); err != nil {
return fmt.Errorf("updating offline mode for frame %v: %w", fs.targetID, err)
}
}

return nil
}

func (fs *FrameSession) throttleNetwork(networkProfile NetworkProfile) error {
Expand Down
8 changes: 5 additions & 3 deletions common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,9 +732,9 @@ func (m *NetworkManager) SetExtraHTTPHeaders(headers network.Headers) {
}

// SetOfflineMode toggles offline mode on/off.
func (m *NetworkManager) SetOfflineMode(offline bool) {
func (m *NetworkManager) SetOfflineMode(offline bool) error {
if m.offline == offline {
return
return nil
}
m.offline = offline

Expand All @@ -745,8 +745,10 @@ func (m *NetworkManager) SetOfflineMode(offline bool) {
m.networkProfile.Upload,
)
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
k6ext.Panic(m.ctx, "setting offline mode: %w", err)
return fmt.Errorf("emulating network conditions: %w", err)
}

return nil
}

// ThrottleNetwork changes the network attributes in chrome to simulate slower
Expand Down
8 changes: 6 additions & 2 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,19 @@ func (p *Page) updateGeolocation() error {
return nil
}

func (p *Page) updateOffline() {
func (p *Page) updateOffline() error {
p.logger.Debugf("Page:updateOffline", "sid:%v", p.sessionID())

p.frameSessionsMu.RLock()
defer p.frameSessionsMu.RUnlock()

for _, fs := range p.frameSessions {
fs.updateOffline(false)
if err := fs.updateOffline(false); err != nil {
return fmt.Errorf("updating page frame sessions to offline: %w", err)
}
}

return nil
}

func (p *Page) updateHTTPCredentials() error {
Expand Down

0 comments on commit 0d66ab4

Please sign in to comment.