// Copyright (C) 2017-2021  Nexedi SA and Contributors.
//                          Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.

package neo
// misc testing utilities.

import (
	"context"
	"math"

	"lab.nexedi.com/kirr/neo/go/zodb"
	zfs1 "lab.nexedi.com/kirr/neo/go/zodb/storage/fs1"
	bfs1 "lab.nexedi.com/kirr/neo/go/neo/storage/fs1"
	"lab.nexedi.com/kirr/go123/exc"
)

func xwait(w interface { Wait() error }) {
	err := w.Wait()
	exc.Raiseif(err)
}

func gox(wg interface { Go(func(context.Context) error) }, xf func(context.Context)) {
	wg.Go(func(ctx context.Context) error {
		return exc.Funcx(func() {
			xf(ctx)
		})()
	})
}

func xfs1stor(path string) *zfs1.FileStorage {
	stor, _, err := zfs1.Open(context.Background(), path, &zodb.DriverOptions{ReadOnly: true})
	exc.Raiseif(err)
	return stor
}

func xfs1back(path string) *bfs1.Backend {
	back, err := bfs1.Open(context.Background(), path)
	exc.Raiseif(err)
	return back
}

// vclock is a virtual clock
type vclock struct {
	t float64
}

func (c *vclock) monotime() float64 {
	c.t += 1E-2
	return c.t
}

func (c *vclock) tick() {
	t := math.Ceil(c.t)
	if !(t > c.t) {
		t += 1
	}
	c.t = t
}
