package sample

import (
	"context"
	"errors"
	"net"
	"net/http"
	"time"
)

var defaultClient = &http.Client{Timeout: time.Second}

var initCtx, initCancel = context.WithTimeout(context.Background(), time.Second)

var load = func(ctx context.Context, objects []string) {
	step, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()
	for range objects {
		_ = step
	}
}

func catalog() {
	newBCommand("usage", time.Minute, nil, nil)
}

func verify(ctx context.Context, cancel context.CancelCauseFunc) {
	_ = watchVerifyProgress(ctx, time.Minute, cancel)
}

func timeouts(ctx context.Context) {
	_, c1 := context.WithTimeout(ctx, time.Second)
	_, c2 := context.WithDeadline(ctx, time.Now().Add(time.Second))
	_, c3 := context.WithTimeoutCause(ctx, time.Second, errors.New("x"))
	_, c4 := context.WithDeadlineCause(ctx, time.Now(), errors.New("x"))
	c1()
	c2()
	c3()
	c4()
}

func timers() {
	t := time.NewTimer(time.Second)
	t.Stop()
	time.AfterFunc(time.Second, func() {
		end := time.Now().Add(time.Minute) // a callback's own deadline: its own site
		_ = end
	})
	<-time.After(time.Second)
	deadline := time.Now().Add(time.Second) // a loop deadline: its own site
	_ = deadline
}

func deadlines(c net.Conn) {
	_ = c.SetDeadline(time.Now().Add(time.Second))
	_ = c.SetReadDeadline(time.Now().Add(time.Second))
	_ = c.SetWriteDeadline(time.Now().Add(time.Second))
	_ = c.SetDeadline(time.Time{}) // clears: not a bound
}

func httpBounds() {
	c := &http.Client{Timeout: time.Second}
	_ = &http.Server{ReadHeaderTimeout: time.Second}
	c.Timeout = 2 * time.Second
}

func passes(p *P) {
	boundedPass(time.Second)
	p.boundedPass(time.Second)
}

type S struct{ chunks []int }

func (s *S) sized(ctx context.Context) {
	ctx, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()
	for range s.chunks {
		_ = ctx
	}
}
