Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
black-adder committed Nov 12, 2018
1 parent 6e4dd7c commit a3eb125
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 18 deletions.
20 changes: 20 additions & 0 deletions pkg/io/starter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2018 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package io

// Starter is the interface that wraps the basic Start() method.
type Starter interface {
Start() error
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ type ProbabilityCalculator interface {
Calculate(targetQPS, curQPS, prevProbability float64) (newProbability float64)
}

// Func wraps a function of appropriate signature and makes a ProbabilityCalculator from it.
type Func func(targetQPS, curQPS, prevProbability float64) (newProbability float64)
// Calculate wraps a function of appropriate signature and makes a ProbabilityCalculator from it.
type Calculate func(targetQPS, curQPS, prevProbability float64) (newProbability float64)

// Calculate implements Calculator interface.
func (f Func) Calculate(targetQPS, curQPS, prevProbability float64) float64 {
return f(targetQPS, curQPS, prevProbability)
func (c Calculate) Calculate(targetQPS, curQPS, prevProbability float64) float64 {
return c(targetQPS, curQPS, prevProbability)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/stretchr/testify/assert"
)

func TestFunc(t *testing.T) {
c := Func(func(targetQPS, qps, oldProbability float64) float64 {
func TestCalculate(t *testing.T) {
c := Calculate(func(targetQPS, qps, oldProbability float64) float64 {
return targetQPS
})
val := 1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCalculate(t *testing.T) {
func TestPercentageIncreaseCappedCalculator(t *testing.T) {
calculator := NewPercentageIncreaseCappedCalculator(0)
tests := []struct {
targetQPS float64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package internal
package leaderelection

import (
"sync"
Expand All @@ -31,7 +31,6 @@ var (
// ElectionParticipant partakes in leader election to become leader.
type ElectionParticipant interface {
IsLeader() bool
Start()
}

type electionParticipant struct {
Expand All @@ -54,17 +53,18 @@ type ElectionParticipantOptions struct {
func NewElectionParticipant(lock dl.Lock, resourceName string, options ElectionParticipantOptions) ElectionParticipant {
return &electionParticipant{
ElectionParticipantOptions: options,
lock: lock,
resourceName: resourceName,
isLeader: atomic.NewBool(false),
closeChan: make(chan struct{}),
lock: lock,
resourceName: resourceName,
isLeader: atomic.NewBool(false),
closeChan: make(chan struct{}),
}
}

// Start runs a background thread which attempts to acquire the leader lock.
func (p *electionParticipant) Start() {
func (p *electionParticipant) Start() error {
p.wg.Add(1)
go p.runAcquireLockLoop()
return nil
}

// Close implements io.Closer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package internal
package leaderelection

import (
"errors"
Expand All @@ -26,6 +26,7 @@ import (
"go.uber.org/atomic"

lmocks "github.com/jaegertracing/jaeger/pkg/distributedlock/mocks"
jio "github.com/jaegertracing/jaeger/pkg/io"
"github.com/jaegertracing/jaeger/pkg/testutils"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ func TestAcquireLock(t *testing.T) {
ElectionParticipantOptions: ElectionParticipantOptions{
LeaderLeaseRefreshInterval: leaderInterval,
FollowerLeaseRefreshInterval: followerInterval,
Logger: logger,
Logger: logger,
},
lock: mockLock,
resourceName: "sampling_lock",
Expand All @@ -85,14 +86,14 @@ func TestRunAcquireLockLoop_followerOnly(t *testing.T) {
p := NewElectionParticipant(mockLock, "sampling_lock", ElectionParticipantOptions{
LeaderLeaseRefreshInterval: time.Millisecond,
FollowerLeaseRefreshInterval: 5 * time.Millisecond,
Logger: logger,
Logger: logger,
},
)

defer func() {
assert.NoError(t, p.(io.Closer).Close())
}()
go p.Start()
go p.(jio.Starter).Start()

expectedErrorMsg := "Failed to acquire lock"
for i := 0; i < 1000; i++ {
Expand Down

0 comments on commit a3eb125

Please sign in to comment.