Skip to content

proposal: time: add TickerChan method that returns Ticker.C #69611

@ohmpatel1997

Description

@ohmpatel1997

Proposal Details

Background

The Ticker struct currently have exporter C channel which is used to send ticks to.

type Ticker struct {
	C          <-chan Time // The channel on which the ticks are delivered.
	initTicker bool
}

Problem

Currently caller has to access the C channel directly from the struct, which makes it tricky to have a mock implementation of the Ticker and put it behind a custom interface.

Proposal

Add a new method func C() <- time.Time which basically returns the same C channel, which makes it easy to mock the ticker behind a custom interface.

type Ticker struct {
	C        <-chan Time // keep it exported
	initTicker bool
}

// C would return the time channel which receives the ticks
func (t *Ticker) TickerChan() <-chan Time {
      return t.c
}

Note:

we can have similar functionality by composing existing ticker into another struct but its way overkill just to have a mocking functionality like below:

type MyTicker struct{
   time.Ticker
}

func (t *MyTicker) C() <-chan time.Time { return t.Ticker.C }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions