| 
									
										
										
										
											2016-04-30 21:56:43 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  | Copyright 2012 Google Inc.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 singleflight
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"errors"
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"sync"
 | 
					
						
							|  |  |  | 	"sync/atomic"
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestDo(t *testing.T) {
 | 
					
						
							|  |  |  | 	var g Group
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	v, err := g.Do(1, func() (interface{}, error) {
 | 
					
						
							| 
									
										
										
										
											2016-04-30 21:56:43 +01:00
										 |  |  | 		return "bar", nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	if got, want := fmt.Sprintf("%v (%T)", v, v), "bar (string)"; got != want {
 | 
					
						
							|  |  |  | 		t.Errorf("Do = %v; want %v", got, want)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Errorf("Do error = %v", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestDoErr(t *testing.T) {
 | 
					
						
							|  |  |  | 	var g Group
 | 
					
						
							| 
									
										
										
										
											2019-02-27 20:25:02 +07:00
										 |  |  | 	someErr := errors.New("some error")
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	v, err := g.Do(1, func() (interface{}, error) {
 | 
					
						
							| 
									
										
										
										
											2016-04-30 21:56:43 +01:00
										 |  |  | 		return nil, someErr
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 	if err != someErr {
 | 
					
						
							|  |  |  | 		t.Errorf("Do error = %v; want someErr", err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if v != nil {
 | 
					
						
							| 
									
										
										
										
											2018-06-02 19:48:39 +01:00
										 |  |  | 		t.Errorf("Unexpected non-nil value %#v", v)
 | 
					
						
							| 
									
										
										
										
											2016-04-30 21:56:43 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestDoDupSuppress(t *testing.T) {
 | 
					
						
							|  |  |  | 	var g Group
 | 
					
						
							|  |  |  | 	c := make(chan string)
 | 
					
						
							|  |  |  | 	var calls int32
 | 
					
						
							|  |  |  | 	fn := func() (interface{}, error) {
 | 
					
						
							|  |  |  | 		atomic.AddInt32(&calls, 1)
 | 
					
						
							|  |  |  | 		return <-c, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const n = 10
 | 
					
						
							|  |  |  | 	var wg sync.WaitGroup
 | 
					
						
							| 
									
										
										
										
											2025-05-29 03:50:55 +03:00
										 |  |  | 	for range n {
 | 
					
						
							| 
									
										
										
										
											2016-04-30 21:56:43 +01:00
										 |  |  | 		wg.Add(1)
 | 
					
						
							|  |  |  | 		go func() {
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 			v, err := g.Do(1, fn)
 | 
					
						
							| 
									
										
										
										
											2016-04-30 21:56:43 +01:00
										 |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				t.Errorf("Do error: %v", err)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			if v.(string) != "bar" {
 | 
					
						
							| 
									
										
										
										
											2018-06-02 19:48:39 +01:00
										 |  |  | 				t.Errorf("Got %q; want %q", v, "bar")
 | 
					
						
							| 
									
										
										
										
											2016-04-30 21:56:43 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 			wg.Done()
 | 
					
						
							|  |  |  | 		}()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	time.Sleep(100 * time.Millisecond) // let goroutines above block
 | 
					
						
							|  |  |  | 	c <- "bar"
 | 
					
						
							|  |  |  | 	wg.Wait()
 | 
					
						
							|  |  |  | 	if got := atomic.LoadInt32(&calls); got != 1 {
 | 
					
						
							| 
									
										
										
										
											2018-06-02 19:48:39 +01:00
										 |  |  | 		t.Errorf("Number of calls = %d; want 1", got)
 | 
					
						
							| 
									
										
										
										
											2016-04-30 21:56:43 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 |