Mocking is primarily used in unit testing. An object under test may have dependencies on other (complex) objects. To isolate the behavior of the object you want to replace the other objects by mocks that simulate the behavior of the real objects. This is useful if the real objects are impractical to incorporate into the unit test.
In short, mocking is creating objects that simulate the behavior of real objects.
Concurrency is not the same as parallel programming or parallelism because parallelism is about parallel execution and concurrency is a little bit more bacause Concurrency is about designing your program as a collection of independent processes about designing these processes to eventually run in parallel. Concurrency is good for your code if:
- Group code(and data) by identifying independent tasks
- No race conditions
- No deadlocks
- More workers = faster execution
Concurrency is aboute breaking up a program into independently executing tasks could potentially run at the same time and still getting the right result at the end so concurrent program can be parallelized. In golang you can run methods by go like this
fun main(){
c := make(chan sting)
go count("ship", c)
msg := <- c
fmt.Pringln(msg)
}
func count(thing string, c chan sting){
for i := 1; i<5 i++ {
c <- thing
time.Sleep(time.Millisecond * 500)
}
}
In this code go means goroutine So lock at this code :
fun main(){
jobs := make(chan int, 100)
results := make(chan int, 100)
go worker(jobs, results)
go worker(jobs, results)
go worker(jobs, results)
go worker(jobs, results)
for i := 0; i< 100; i++{
jobs <- i
}
close(jobs)
for j := 0; j< 100; j++{
fmt.Println(<-results)
}
}
func worker(jobs <-chan int, results chan<- int){
for n := range jobs {
results <- fib(n)
}
}
fun fib(n){
if n <= 1{
return n
}
return fib(n-1) + fib(n-2)
}