Skip to content
quarry Artifact [f9fb4e1f52]
Login

Artifact [f9fb4e1f52]

Artifact f9fb4e1f525e4484d09f81c33728e347913917bb32e554f3cce4dfcaee7f86cf:


package web

import (
	"bufio"
	"context"
	"net/http"
	"net/http/httptest"
	"path/filepath"
	"strings"
	"sync"
	"testing"
	"time"

	"craftdesign.group/quarry/internal/api"
	"craftdesign.group/quarry/internal/index"
	"craftdesign.group/quarry/internal/ops"
)

// repoTicker is a Notifier that says which repository each tick is for,
// as the events plane does.
type repoTicker struct {
	mu sync.Mutex
	fn func(repo string)
}

func (*repoTicker) Watch(string) (<-chan struct{}, func()) {
	// The coarse every-repository signal: the front page must not use it.
	ch := make(chan struct{}, 1)
	ch <- struct{}{}
	return ch, func() {}
}

func (r *repoTicker) WatchRepos(fn func(repo string)) func() {
	r.mu.Lock()
	r.fn = fn
	r.mu.Unlock()
	return func() {}
}

func (r *repoTicker) tick(repo string) {
	r.mu.Lock()
	fn := r.fn
	r.mu.Unlock()
	if fn != nil {
		fn(repo)
	}
}

// The front page's revision is the viewer's own: it moves for a
// repository the viewer may list and never for one they may not (a held
// or private one), so a watcher cannot see that a repository they cannot
// list had activity.
func TestFrontPageRevisionMovesOnlyForListableRepositories(t *testing.T) {
	mk := func() *api.Repo {
		ix, err := index.Open(filepath.Join(t.TempDir(), "index.db"))
		if err != nil {
			t.Fatal(err)
		}
		t.Cleanup(func() { ix.Close() })
		return &api.Repo{Index: ix}
	}
	r := ops.NewRegistry()
	if err := api.RegisterAll(r, api.Deps{Version: "v",
		Repos:   api.NewRepos(map[string]*api.Repo{"open": mk(), "sealed": mk()}),
		CanRead: func(_ context.Context, repo string) bool { return repo != "sealed" }}); err != nil {
		t.Fatal(err)
	}
	n := &repoTicker{}
	srv := httptest.NewServer(Handler(r, n, nil))
	t.Cleanup(srv.Close)

	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
	defer cancel()
	req, _ := http.NewRequestWithContext(ctx, "GET", srv.URL+"/explore.rev", nil)
	resp, err := srv.Client().Do(req)
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { resp.Body.Close() })
	signals := make(chan string, 16)
	go func() {
		sc := bufio.NewScanner(resp.Body)
		for sc.Scan() {
			if strings.Contains(sc.Text(), "fleetRev") {
				signals <- sc.Text()
			}
		}
		close(signals)
	}()
	// The subscription is made once the stream opens.
	deadline := time.Now().Add(5 * time.Second)
	for {
		n.mu.Lock()
		ready := n.fn != nil
		n.mu.Unlock()
		if ready || time.Now().After(deadline) {
			break
		}
		time.Sleep(10 * time.Millisecond)
	}

	n.tick("sealed")
	select {
	case s := <-signals:
		t.Errorf("FAIL: the revision moved for a repository the viewer cannot list: %s", s)
	case <-time.After(1500 * time.Millisecond):
	}
	n.tick("open")
	select {
	case <-signals:
	case <-time.After(5 * time.Second):
		t.Error("FAIL: the revision did not move for a repository the viewer can list")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}