...

Source file src/github.com/go-kivik/kivik/v4/kiviktest/client/getreplications.go

Documentation: github.com/go-kivik/kivik/v4/kiviktest/client

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package client
    14  
    15  import (
    16  	"context"
    17  	"sync"
    18  
    19  	"gitlab.com/flimzy/testy"
    20  
    21  	kivik "github.com/go-kivik/kivik/v4"
    22  	"github.com/go-kivik/kivik/v4/kiviktest/kt"
    23  )
    24  
    25  func init() {
    26  	kt.Register("GetReplications", getReplications)
    27  }
    28  
    29  // masterMU protects the map
    30  var masterMU sync.Mutex
    31  
    32  // We can only run one set of replication tests at a time
    33  var replicationMUs = make(map[*kivik.Client]*sync.Mutex)
    34  
    35  func lockReplication(ctx *kt.Context) func() {
    36  	masterMU.Lock()
    37  	defer masterMU.Unlock()
    38  	if _, ok := replicationMUs[ctx.Admin]; !ok {
    39  		replicationMUs[ctx.Admin] = &sync.Mutex{}
    40  	}
    41  	replicationMUs[ctx.Admin].Lock()
    42  	return func() { replicationMUs[ctx.Admin].Unlock() }
    43  }
    44  
    45  func getReplications(ctx *kt.Context) {
    46  	defer lockReplication(ctx)()
    47  	ctx.RunAdmin(func(ctx *kt.Context) {
    48  		ctx.Parallel()
    49  		testGetReplications(ctx, ctx.Admin, []struct{}{})
    50  	})
    51  	ctx.RunNoAuth(func(ctx *kt.Context) {
    52  		ctx.Parallel()
    53  		testGetReplications(ctx, ctx.NoAuth, []struct{}{})
    54  	})
    55  	ctx.RunRW(func(ctx *kt.Context) {
    56  		ctx.RunAdmin(func(ctx *kt.Context) {
    57  			ctx.Parallel()
    58  		})
    59  		ctx.RunNoAuth(func(ctx *kt.Context) {
    60  			ctx.Parallel()
    61  		})
    62  	})
    63  }
    64  
    65  func testGetReplications(ctx *kt.Context, client *kivik.Client, expected interface{}) {
    66  	reps, err := client.GetReplications(context.Background())
    67  	if !ctx.IsExpectedSuccess(err) {
    68  		return
    69  	}
    70  	if d := testy.DiffAsJSON(expected, reps); d != nil {
    71  		ctx.Errorf("GetReplications results differ:\n%s\n", d)
    72  	}
    73  }
    74  

View as plain text