How to use Last method of td Package

Best Go-testdeep code snippet using td.Last

trace.go

Source:trace.go Github

copy

Full Screen

...20)21var TraceTmpl = template.Must(template.Must(BaseTemplate.Clone()).Parse(`22{{define "title"}}Trace Information{{end}}23{{define "body"}}24 {{range .Traces}}<a href="/trace/{{.Name}}">{{.Name}}</a> last: {{.Last.Duration}}, longest: {{.Longest.Duration}}<br>{{end}}25 {{if .Selected}}26 <H2>{{.Selected.Name}}</H2>27 {{if .Selected.Last}}<H3>Last</H3><ul>{{template "details" .Selected.Last}}</ul>{{end}}28 {{if .Selected.Longest}}<H3>Longest</H3><ul>{{template "details" .Selected.Longest}}</ul>{{end}}29 {{end}}30{{end}}31{{define "details"}}32 <li>{{.Offset}} {{.Name}} {{.Duration}} {{.Tags}}</li>33 {{if .Events}}<ul class=events>{{range .Events}}<li>{{.Offset}} {{.Tags}}</li>{{end}}</ul>{{end}}34 {{if .Children}}<ul>{{range .Children}}{{template "details" .}}{{end}}</ul>{{end}}35{{end}}36`))37type traces struct {38 mu sync.Mutex39 sets map[string]*traceSet40 unfinished map[export.SpanContext]*traceData41}42type TraceResults struct { // exported for testing43 Traces []*traceSet44 Selected *traceSet45}46type traceSet struct {47 Name string48 Last *traceData49 Longest *traceData50}51type traceData struct {52 TraceID export.TraceID53 SpanID export.SpanID54 ParentID export.SpanID55 Name string56 Start time.Time57 Finish time.Time58 Offset time.Duration59 Duration time.Duration60 Tags string61 Events []traceEvent62 Children []*traceData63}64type traceEvent struct {65 Time time.Time66 Offset time.Duration67 Tags string68}69func StdTrace(exporter event.Exporter) event.Exporter {70 return func(ctx context.Context, ev core.Event, lm label.Map) context.Context {71 span := export.GetSpan(ctx)72 if span == nil {73 return exporter(ctx, ev, lm)74 }75 switch {76 case event.IsStart(ev):77 if span.ParentID.IsValid() {78 region := trace.StartRegion(ctx, span.Name)79 ctx = context.WithValue(ctx, traceKey, region)80 } else {81 var task *trace.Task82 ctx, task = trace.NewTask(ctx, span.Name)83 ctx = context.WithValue(ctx, traceKey, task)84 }85 // Log the start event as it may contain useful labels.86 msg := formatEvent(ctx, ev, lm)87 trace.Log(ctx, "start", msg)88 case event.IsLog(ev):89 category := ""90 if event.IsError(ev) {91 category = "error"92 }93 msg := formatEvent(ctx, ev, lm)94 trace.Log(ctx, category, msg)95 case event.IsEnd(ev):96 if v := ctx.Value(traceKey); v != nil {97 v.(interface{ End() }).End()98 }99 }100 return exporter(ctx, ev, lm)101 }102}103func formatEvent(ctx context.Context, ev core.Event, lm label.Map) string {104 buf := &bytes.Buffer{}105 p := export.Printer{}106 p.WriteEvent(buf, ev, lm)107 return buf.String()108}109func (t *traces) ProcessEvent(ctx context.Context, ev core.Event, lm label.Map) context.Context {110 t.mu.Lock()111 defer t.mu.Unlock()112 span := export.GetSpan(ctx)113 if span == nil {114 return ctx115 }116 switch {117 case event.IsStart(ev):118 if t.sets == nil {119 t.sets = make(map[string]*traceSet)120 t.unfinished = make(map[export.SpanContext]*traceData)121 }122 // just starting, add it to the unfinished map123 td := &traceData{124 TraceID: span.ID.TraceID,125 SpanID: span.ID.SpanID,126 ParentID: span.ParentID,127 Name: span.Name,128 Start: span.Start().At(),129 Tags: renderLabels(span.Start()),130 }131 t.unfinished[span.ID] = td132 // and wire up parents if we have them133 if !span.ParentID.IsValid() {134 return ctx135 }136 parentID := export.SpanContext{TraceID: span.ID.TraceID, SpanID: span.ParentID}137 parent, found := t.unfinished[parentID]138 if !found {139 // trace had an invalid parent, so it cannot itself be valid140 return ctx141 }142 parent.Children = append(parent.Children, td)143 case event.IsEnd(ev):144 // finishing, must be already in the map145 td, found := t.unfinished[span.ID]146 if !found {147 return ctx // if this happens we are in a bad place148 }149 delete(t.unfinished, span.ID)150 td.Finish = span.Finish().At()151 td.Duration = span.Finish().At().Sub(span.Start().At())152 events := span.Events()153 td.Events = make([]traceEvent, len(events))154 for i, event := range events {155 td.Events[i] = traceEvent{156 Time: event.At(),157 Tags: renderLabels(event),158 }159 }160 set, ok := t.sets[span.Name]161 if !ok {162 set = &traceSet{Name: span.Name}163 t.sets[span.Name] = set164 }165 set.Last = td166 if set.Longest == nil || set.Last.Duration > set.Longest.Duration {167 set.Longest = set.Last168 }169 if !td.ParentID.IsValid() {170 fillOffsets(td, td.Start)171 }172 }173 return ctx174}175func (t *traces) getData(req *http.Request) interface{} {176 if len(t.sets) == 0 {177 return nil178 }179 data := TraceResults{}180 data.Traces = make([]*traceSet, 0, len(t.sets))181 for _, set := range t.sets {...

Full Screen

Full Screen

timer.go

Source:timer.go Github

copy

Full Screen

1package time2import (3 "sync"4 itime "time"5 "go-common/library/log"6)7const (8 timerFormat = "2006-01-02 15:04:05"9 infiniteDuration = itime.Duration(1<<63 - 1)10)11var (12 timerLazyDelay = 300 * itime.Millisecond13)14// TimerData timer data.15type TimerData struct {16 Key string17 expire itime.Time18 fn func()19 index int20 next *TimerData21}22// Delay delay duration.23func (td *TimerData) Delay() itime.Duration {24 return td.expire.Sub(itime.Now())25}26// ExpireString expire string.27func (td *TimerData) ExpireString() string {28 return td.expire.Format(timerFormat)29}30// Timer timer.31type Timer struct {32 lock sync.Mutex33 free *TimerData34 timers []*TimerData35 signal *itime.Timer36 num int37}38// NewTimer new a timer.39// A heap must be initialized before any of the heap operations40// can be used. Init is idempotent with respect to the heap invariants41// and may be called whenever the heap invariants may have been invalidated.42// Its complexity is O(n) where n = h.Len().43//44func NewTimer(num int) (t *Timer) {45 t = new(Timer)46 t.init(num)47 return t48}49// Init init the timer.50func (t *Timer) Init(num int) {51 t.init(num)52}53func (t *Timer) init(num int) {54 t.signal = itime.NewTimer(infiniteDuration)55 t.timers = make([]*TimerData, 0, num)56 t.num = num57 t.grow()58 go t.start()59}60func (t *Timer) grow() {61 var (62 i int63 td *TimerData64 tds = make([]TimerData, t.num)65 )66 t.free = &(tds[0])67 td = t.free68 for i = 1; i < t.num; i++ {69 td.next = &(tds[i])70 td = td.next71 }72 td.next = nil73}74// get get a free timer data.75func (t *Timer) get() (td *TimerData) {76 if td = t.free; td == nil {77 t.grow()78 td = t.free79 }80 t.free = td.next81 return82}83// put put back a timer data.84func (t *Timer) put(td *TimerData) {85 td.fn = nil86 td.next = t.free87 t.free = td88}89// Add add the element x onto the heap. The complexity is90// O(log(n)) where n = h.Len().91func (t *Timer) Add(expire itime.Duration, fn func()) (td *TimerData) {92 t.lock.Lock()93 td = t.get()94 td.expire = itime.Now().Add(expire)95 td.fn = fn96 t.add(td)97 t.lock.Unlock()98 return99}100// Del removes the element at index i from the heap.101// The complexity is O(log(n)) where n = h.Len().102func (t *Timer) Del(td *TimerData) {103 t.lock.Lock()104 t.del(td)105 t.put(td)106 t.lock.Unlock()107}108// Push pushes the element x onto the heap. The complexity is109// O(log(n)) where n = h.Len().110func (t *Timer) add(td *TimerData) {111 var d itime.Duration112 td.index = len(t.timers)113 // add to the minheap last node114 t.timers = append(t.timers, td)115 t.up(td.index)116 if td.index == 0 {117 // if first node, signal start goroutine118 d = td.Delay()119 t.signal.Reset(d)120 if Debug {121 log.Info("timer: add reset delay %d ms", int64(d)/int64(itime.Millisecond))122 }123 }124 if Debug {125 log.Info("timer: push item key: %s, expire: %s, index: %d", td.Key, td.ExpireString(), td.index)126 }127}128func (t *Timer) del(td *TimerData) {129 var (130 i = td.index131 last = len(t.timers) - 1132 )133 if i < 0 || i > last || t.timers[i] != td {134 // already remove, usually by expire135 if Debug {136 log.Info("timer del i: %d, last: %d, %p", i, last, td)137 }138 return139 }140 if i != last {141 t.swap(i, last)142 t.down(i, last)143 t.up(i)144 }145 // remove item is the last node146 t.timers[last].index = -1 // for safety147 t.timers = t.timers[:last]148 if Debug {149 log.Info("timer: remove item key: %s, expire: %s, index: %d", td.Key, td.ExpireString(), td.index)150 }151}152// Set update timer data.153func (t *Timer) Set(td *TimerData, expire itime.Duration) {154 t.lock.Lock()155 t.del(td)156 td.expire = itime.Now().Add(expire)157 t.add(td)158 t.lock.Unlock()159}160// start start the timer.161func (t *Timer) start() {162 for {163 t.expire()164 <-t.signal.C165 }166}167// expire removes the minimum element (according to Less) from the heap.168// The complexity is O(log(n)) where n = max.169// It is equivalent to Del(0).170func (t *Timer) expire() {171 var (172 fn func()173 td *TimerData174 d itime.Duration175 )176 t.lock.Lock()177 for {178 if len(t.timers) == 0 {179 d = infiniteDuration180 if Debug {181 log.Info("timer: no other instance")182 }183 break184 }185 td = t.timers[0]186 if d = td.Delay(); d > 0 {187 break188 }189 fn = td.fn190 // let caller put back191 t.del(td)192 t.lock.Unlock()193 if fn == nil {194 log.Warn("expire timer no fn")195 } else {196 if Debug {197 log.Info("timer key: %s, expire: %s, index: %d expired, call fn", td.Key, td.ExpireString(), td.index)198 }199 fn()200 }201 t.lock.Lock()202 }203 t.signal.Reset(d)204 if Debug {205 log.Info("timer: expier reset delay %d ms", int64(d)/int64(itime.Millisecond))206 }207 t.lock.Unlock()208}209func (t *Timer) up(j int) {210 for {211 i := (j - 1) / 2 // parent212 if i <= j || !t.less(j, i) {213 break214 }215 t.swap(i, j)216 j = i217 }218}219func (t *Timer) down(i, n int) {220 for {221 j1 := 2*i + 1222 if j1 >= n || j1 < 0 { // j1 < 0 after int overflow223 break224 }225 j := j1 // left child226 if j2 := j1 + 1; j2 < n && !t.less(j1, j2) {227 j = j2 // = 2*i + 2 // right child228 }229 if !t.less(j, i) {230 break231 }232 t.swap(i, j)233 i = j234 }235}236func (t *Timer) less(i, j int) bool {237 return t.timers[i].expire.Before(t.timers[j].expire)238}239func (t *Timer) swap(i, j int) {240 t.timers[i], t.timers[j] = t.timers[j], t.timers[i]241 t.timers[i].index = i242 t.timers[j].index = j243}...

Full Screen

Full Screen

Last

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 data := [][]string{4 []string{"1", "2", "3", "4", "5", "6"},5 []string{"7", "8", "9", "10", "11", "12"},6 []string{"13", "14", "15", "16", "17", "18"},7 []string{"19", "20", "21", "22", "23", "24"},8 }9 table := tablewriter.NewWriter(os.Stdout)10 table.SetHeader([]string{"A", "B", "C", "D", "E", "F"})11 table.AppendBulk(data)12 table.Render()13}

Full Screen

Full Screen

Last

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 xlFile, err := xlsx.OpenFile("Book1.xlsx")4 if err != nil {5 fmt.Println(err)6 }7 for _, sheet := range xlFile.Sheets {8 for _, row := range sheet.Rows {9 for _, cell := range row.Cells {10 text := cell.String()11 fmt.Printf("%s12 }13 }14 fmt.Println("Last row number: ", sheet.MaxRow)15 }16}17func (r *Row) Next() *Cell18import (19func main() {20 xlFile, err := xlsx.OpenFile("Book1.xlsx")21 if err != nil {22 fmt.Println(err)23 }24 for _, sheet := range xlFile.Sheets {25 for _, row := range sheet.Rows {26 for _, cell := range row.Cells {27 text := cell.String()28 fmt.Printf("%s29 }30 fmt.Println("Next cell: ", row.Next())31 }32 }33}34Next cell: &{0xc0000a4a80 0xc0000a4b00 0xc0000a4b80 0xc0000a4c00 0xc0000a4c80 0xc0000a4d00 0xc0000a4d80 0xc0000a4e00 0xc0000a4e80 0xc0000a4f00 0xc0000a4f80 0xc0000a5000 0xc0000a5080 0xc0000a5100 0xc000

Full Screen

Full Screen

Last

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 data := [][]string{4 []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"},5 []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"},6 []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"},7 []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"},8 []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"},9 []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"},10 []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"},11 []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"},12 []string{"1", "2", "3", "4", "5", "6", "7", "8", "9"},13 }14 table := tablewriter.NewWriter(os.Stdout)15 table.SetHeader([]string{"A", "B", "C", "D", "E", "F", "G", "H", "I"})16 table.SetFooter([]string{"A", "B", "

Full Screen

Full Screen

Last

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "github.com/olekukonko/tablewriter"3func main() {4table := tablewriter.NewWriter(os.Stdout)5table.SetHeader([]string{"Name", "Age"})6table.Append([]string{"John", "21"})7table.Append([]string{"Jack", "22"})8table.Append([]string{"Oliver", "23"})9table.Append([]string{"Mark", "24"})10table.Append([]string{"Harry", "25"})11table.Append([]string{"Jacob", "26"})12table.Append([]string{"Charlie", "27"})13table.Append([]string{"Thomas", "28"})14table.Append([]string{"George", "29"})15table.Append([]string{"Oscar", "30"})16table.Append([]string{"James", "31"})17table.Append([]string{"William", "32"})18table.Append([]string{"Henry", "33"})19table.Append([]string{"Alfie", "34"})20table.Append([]string{"Joshua", "35"})21table.Append([]string{"Muhammad", "36"})22table.Append([]string{"Ethan", "37"})23table.Append([]string{"Noah", "38"})24table.Append([]string{"Daniel", "39"})25table.Append([]string{"Arthur", "40"})26table.Append([]string{"Leo", "41"})27table.Append([]string{"Freddie", "42"})28table.Append([]string{"Alexander", "43"})29table.Append([]string{"Joseph", "44"})30table.Append([]string{"Max", "45"})31table.Append([]string{"Samuel", "46"})32table.Append([]string{"Jake", "47"})33table.Append([]string{"Dylan", "48"})34table.Append([]string{"Adam", "49"})35table.Append([]string{"Mohammed", "50"})36table.Append([]string{"The last name is", table.Last()})37table.Render()38}

Full Screen

Full Screen

Last

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 td = append(td, "a")4 td = append(td, "b")5 td = append(td, "c")6 fmt.Println(td.Last())7}8import "fmt"9func main() {10 td = append(td, "a")11 td = append(td, "b")12 td = append(td, "c")13 fmt.Println(td.Last())14}15import "fmt"16func main() {17 td = append(td, "a")18 td = append(td, "b")19 td = append(td, "c")20 fmt.Println(td.Last())21}22import "fmt"23func main() {24 td = append(td, "a")25 td = append(td, "b")26 td = append(td, "c")27 fmt.Println(td.Last())28}29import "fmt"30func main() {31 td = append(td, "a")32 td = append(td, "b")33 td = append(td, "c")34 fmt.Println(td.Last())35}36import "fmt"37func main() {38 td = append(td, "a")39 td = append(td, "b")40 td = append(td, "c")41 fmt.Println(td.Last())42}43import "fmt"44func main() {45 td = append(td, "a")46 td = append(td

Full Screen

Full Screen

Last

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 td := xlsx.NewFile()4 td.NewSheet("Sheet1")5 td.Sheet["Sheet1"].AddRow()6 td.Sheet["Sheet1"].Row[0].AddCell()7 td.Sheet["Sheet1"].Row[0].AddCell()8 td.Sheet["Sheet1"].Row[0].AddCell()9 err := td.Save("2.xlsx")10 if err != nil {11 fmt.Printf(err.Error())12 }13}

Full Screen

Full Screen

Last

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 t = td.New()4 t.Push(3)5 t.Push(4)6 t.Push(5)7 t.Push(6)8 t.Push(7)9 t.Push(8)10 fmt.Println(t.Last())11}

Full Screen

Full Screen

Last

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 s1 := td.NewStack()4 s1.Push(1)5 s1.Push(2)6 fmt.Println(s1.Last())7}8import (9func main() {10 s1 := td.NewStack()11 s1.Push(1)12 s1.Push(2)13 fmt.Println(s1.Pop())14 fmt.Println(s1.Pop())15 fmt.Println(s1.Pop())16}17import (18func main() {19 s1 := td.NewStack()20 s1.Push(1)21 s1.Push(2)22 fmt.Println(s1.IsEmpty())23 s1.Pop()24 s1.Pop()25 fmt.Println(s1.IsEmpty())26}27import (28func main() {29 s1 := td.NewStack()30 s1.Push(1)31 s1.Push(2)32 fmt.Println(s1.Size())33 s1.Pop()34 s1.Pop()35 fmt.Println(s1.Size())36}37import (38func main() {39 s1 := td.NewStack()40 s1.Push(1)41 s1.Push(2)42 fmt.Println(s1.Size())43 s1.Clear()44 fmt.Println(s1.Size())45}46import (47func main() {48 s1 := td.NewStack()49 s1.Push(1)50 s1.Push(2)51 fmt.Println(s1.String())52}53import (54func main() {55 s1 := td.NewStack()56 s2 := td.NewStack()57 s2.Push(1)58 s2.Push(2)59 s1.PushAll(s2)60 fmt.Println(s1.String

Full Screen

Full Screen

Last

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 t := time.Now()4 d := t.Weekday()5 fmt.Println(d)6 fmt.Println(d.Last())7}8import (9func main() {10 t := time.Now()11 t = t.AddDate(2, 3, 4)12 fmt.Println(t)13}14import (15func main() {16 t := time.Now()17 t = t.Add(2 * time.Hour)18 fmt.Println(t)19}20import (21func main() {22 t1 := time.Now()23 t2 := t1.Add(2 * time.Hour)24 fmt.Println(t1.Before(t2))25 fmt.Println(t1.After(t2))26}27import (28func main() {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Go-testdeep automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful